[英]Style each element in ng-repeat differently
我有一個ng-repeat
和內部的ng-repeat
每個備選div有不同的border-color
。 所以我用過
< div ng-repeat="channel in channelList">
<div ng-style="getBgColor()">
</div
getBgColor()
定義為:
$scope.currentColorIndex = (($scope.currentColorIndex+1) % $scope.radioColors.length);
$scope.tileColor = $scope.radioColors[$scope.currentColorIndex].hex;
return $scope.tileColor
我不斷收到錯誤
$rootScope:infdig] 10 $digest() iterations reached
我知道為什么會出錯,因為在每次ng-repeat
迭代中,我都會返回一個不同的對象。 解決該問題的方法是什么?
在這里,您甚至不需要調用函數,因為ng-repeat
使用$index
記錄其$index
您可以在內部元素中使用它。
<div ng-repeat="channel in channelList">
<div ng-style="{color: radioColors[$index].hex}">
</div
它將color
屬性分配給radioColors[$index].hex
表達式返回的十六進制,其中radioColors
是作用域對象。
使用ngRepeat允許您使用$ index。
<div ng-repeat="channel in channelList">
<div ng-style="getBgColor($index)">
</div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.