bobighorus
bobighorus

Reputation: 1152

Parse JSON array values with AngularJs to text strings

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.

Upvotes: 2

Views: 2478

Answers (1)

mohamedrias
mohamedrias

Reputation: 18566

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>

Upvotes: 2

Related Questions