Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I need to access to the values inside the array "position" of this json file using AngularJS. My problem is that I need to get them as text string values, because I need to add them as css class name inside a tag, basically like this:

<div class="europe italy rome"></div>
<div class="europe france paris></div>
<div class="america usa atlanta></div>

This is my Json file:

 [
      {
        "name": "Rome",
        "position": ["europe", "italy", "rome"]
      },
      {
        "name": "Paris",
        "position": ["europe", "france", "paris"]
      },
      {
        "name": "Rome",
        "position": ["america", "usa", "atlanta"]
      }
]

I'm not able to figure it out how to do this. Thank you in advance for your suggestions.

share|improve this question
up vote 3 down vote accepted

You can just join the text inside array.

angular.module("app",[])
.controller("MainCtrl", function($scope) {
   $scope.styles = [
  {
"name": "Rome",
"position": ["europe", "italy", "rome"]
  },
  {
"name": "Paris",
"position": ["europe", "france", "paris"]
  },
  {
"name": "Rome",
"position": ["america", "usa", "atlanta"]
  }
];

});
<html ng-app="app">
<head>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body ng-controller="MainCtrl">

 <div ng-repeat="style in styles" ng-class="style.position.join(' ')">
   applied style : {{style.position.join(' ')}}
 </div>
</body>
</html>

share|improve this answer
    
Great solution! Simple and effective! Thank you @mohamedrias! – bobighorus Apr 11 '15 at 18:15
    
You are welcome :) – mohamedrias Apr 11 '15 at 18:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.