user3739703
user3739703

Reputation: 251

How to access particular item in array using $index (angular)

This seems like it should be so simple but my brain is exploding. This is what I am trying to achieve using angular, I have my shopping list

app.controller("Destroyer", function($scope) {
    $scope.shoppingList = [
      {name: 'Milk'},
      {name: 'Eggs'},
      {name: 'Bread'},
      {name: 'Cheese'},
      {name: 'Ham'}
    ];

});

In my template I want to access the the the 3rd item for example

{{ shoppingList.name[$index == 2] }}

How do I do this? Thanks

EDIT: Answer {{ shoppingList[2].name }}

Upvotes: 0

Views: 8011

Answers (1)

Francis Stalin
Francis Stalin

Reputation: 449

if you know the index you can give like this

{{shoppingList[2].name}}

or in ng-repeat 

<div ng-repeat="list in shoppingList " ng-show="$index==2">{{list.name}}</div>

Upvotes: 1

Related Questions