0

I want to retrieve a specific value in a multidimensional object in angular js. My object is something like this:

 $Item =   [{item1: 'lorem ipsum'}, {item2: [{inner_item:'lorem ipsum', inner_item2: 'lorem ipsum'}]},...]

How can I accomplish this in a controller by pointing out like in jquery:

$Item[1][2];
and also in the expression 
{{???}}

2 Answers 2

1

Your data is a little ambiguous but I will show you how to do two variations the proper Angular way:

var app = angular.module('myApp', []);

app.controller('TestCtrl', ['$scope', function($scope) {
  $scope.items =  [{country: 'Germany'}, 
                   {country: 'Spain'}, 
                   {country: 'England'}];
  $scope.nestedItems = [{continent: 'Europe', 
                         countries: [
                           {country: 'Germany'}, 
                           {country: 'Spain'}, 
                           {country: 'England'}]
                        }];
}]);
.single {
    background-color: lightgreen;
}

.nested {
    background-color: lightblue;  
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js">

</script>
<section ng-app="myApp" ng-controller="TestCtrl">
  <div class="single">
   <ul>
    <li ng-repeat="item in items"> 
      Country: {{item.country}}
    </li>
  </ul>
  </div>
  <div class="nested" ng-repeat="item in nestedItems">
    <h2>Continent: {{item.continent}}</h2>
    <ul>
    <li ng-repeat="item in item.countries"> 
      Country: {{item.country}}
    </li>
  </ul>
  </div>
  
  
</section>

Sign up to request clarification or add additional context in comments.

Comments

0
  • array items can be accessed by square notation (ex: item[0])
  • object parameters can be accessed by dot notation (ex: item1.paramater)

var app = angular.module('app', []).controller('indexCtrl', function ($scope) {
    $scope.item = [
      {item1: 'lorem ipsum'},
      {item2: [{inner_item:'lorem ipsum', inner_item2: 'lorem ipsum'}]
      }];  
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="app" ng-controller="indexCtrl">
  <p>item[0]: {{item[0]}}</p>
  <p>item[0].item1: {{item[0].item1}}</p>
  <p>item[1]: {{item[1]}}</p>
  <p>item[1].item2[0]: {{item[1].item2[0]}}</p>
  <p>item[1].item2[0].inner_item: {{item[1].item2[0].inner_item}}</p>
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.