angularjs中$ index的确切用法是什么?我在angularjs中使用$ index时几乎没有混淆
<div class="row review" ng-show="mode=='review'">
<div class="col-sm-4" ng-repeat="question in questions">
<div ng-click="goTo($index + 1)" class="{{ isAnswered($index) == 'Answered'? 'answered': 'not-answered' }}">{{$index + 1}}. {{ isAnswered($index) }}</div>
</div>
</div>
答案 0 :(得分:4)
它们是你在ng-repeat中使用的数组的键。
您可以在doc上看到完整的详细信息。
ngRepeat指令从集合中为每个项目实例化一次模板。每个模板实例都有自己的范围,其中给定的循环变量设置为当前集合项,$ index设置为项索引或键。
您可以在docs上看到示例。
使用$ index:
生成以下结果[1] John who is 25 years old.
[2] Jessie who is 30 years old.
[3] Johanna who is 28 years old.
[4] Joy who is 15 years old.
[5] Mary who is 28 years old.
[6] Peter who is 95 years old.
[7] Sebastian who is 50 years old.
[8] Erika who is 27 years old.
[9] Patrick who is 40 years old.
这里是demo的angularjs代码的一部分:
<li class="animate-repeat" ng-repeat="friend in friends | filter:q as results">
[{{$index + 1}}] {{friend.name}} who is {{friend.age}} years old.
</li>