[英]Speed issues with ngRepeat. Is it possible to reuse a DOM-part in AngularJS?
[英]Checkbox label id increment within ngrepeat.
<div class="checkbox"><input type="checkbox" id="1"><label for="1"></label></div>
我想設置復選框的樣式,因此必須有一個ID和一個標簽,復選框才能正常工作(選中或取消選中)。
我可以通過增量js使用純js進行設置,但這不是有角度的js方法。 我將其放在ngrepeat中,將其視為待辦事項列表,如何使其遞增?
您可以使用ng-repeat的索引部分,即密鑰:
<div class="checkbox" ng-repeat="(index,value) in items">
<input type="checkbox" id="{{index}}"><label for="{{index}}"></label>
</div>
或使用子范圍$index
值
<div class="checkbox" ng-repeat="item in items">
<input type="checkbox" id="{{$index}}"><label for="{{$index}}"></label>
</div>
http://plnkr.co/edit/Yh02rAuH40XU0hB4eQO6?p=preview
兩種方法之間的區別在於,您假定范圍$index
僅足以使項目為序數。 在key
方法中,您實際上可以訪問items數組上的原始索引(但您也可以在$scope.item
訪問它)
您可以使用$ index在數組中查找項目的索引( 小提琴示例 )
<input type="checkbox" id="{{$index}}"/>
<label for="{{$index}}">{{$index}}</label>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.