从ng-repeat ='item in items'中获取最大值

时间:2013-09-21 07:23:07

标签: angularjs

ul
 li(ng-repeat='item in index')
  a(href='{{item}}') link is {{item}}

在控制器

$scope.index= ['1', '2', '3', '4'];

$scope.alphabets= ['a', 'b', 'c', 'd'];

$scope.fruits = ['apple', 'banana', 'coconut', 'dates'] 

以上是有效的,但我们只能在html页面中使用{{item}}。因此,href<a> tag都包含1, 2, 3, 4

{{}}中的{{alphabets[{{item}}]}}进行双重评估不起作用。

我想这样做:

li(ng-repeat='item in index')
 a(href='alphabets[{{item}}]') fruits[{{item}}]

显示

a(href='a') apple

2 个答案:

答案 0 :(得分:3)

您不需要“嵌套”表达式。索引访问可以使用相同的表达式:{{fruits[$index]}}

li(ng-repeat='item in index')
 a(href='{{alphabets[$index]}}') {{fruits[$index]}}

顺便说一下,为什么要对这些数据进行建模。似乎有点hacky。仅创建一个对象数组不是更好吗?

$scope.items = 
  [{letter: 'a', fruit: 'apple'}, {letter: 'b', fruit: 'banana'} /*etc.*/]

答案 1 :(得分:2)

使用$ index属性(即ng-repeat中项目的索引)可以更简单。

<ul ng-repeat="item in index">
      <li><a href="{{alphabets[$index]}}">{{fruits[$index]}}</a></li>
</ul>
相关问题