如何使用$ index(angular)访问数组中的特定项

时间:2014-07-18 06:55:29

标签: angularjs

这似乎应该是如此简单,但我的大脑正在爆炸。这是我想用角度来实现的,我有我的购物清单

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

});

在我的模板中,我想访问第3项,例如

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

我该怎么做?感谢

编辑:回答{{shoppingList [2] .name}}

1 个答案:

答案 0 :(得分:1)

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>