编辑 2(答案):对于遇到此问题并寻找类似问题解决方案的任何人,解决方案都在 Gianmarco 回答下的评论中。该productusage
数组使用与下面的函数相同的函数进行预处理$scope.alternate
,结果用于在数组中的每个项目上设置一个 isAlternate 标志。然后在 ng-style 或 ng-class 属性中使用它来确定在行上使用哪种样式。
编辑:为清楚起见:我有一组 RANames 可能会去... RAFoo,RAFoo,RABar,RABar,RABar,RABar,RABaz,RABaz,RABaz 等...我不希望该行交替颜色,直到它遇到了一个新的 RAName。具有相同 RAName 的 RAName 值的数量是可变的,因此可能有 2 个“RAFoo”、4 个“RABar”和 3 个“RABaz”等。它可以改变。
我的控制器中有以下代码:
var prevRAName = ""; // intialize prevRAName to empty string
var switchColor = false;
...
$scope.alternate = function (currRAName) {
if (prevRAName) { // starts on 2nd row...
if (currRAName == prevRAName) {
prevRAName = currRAName;
} else {
switchColor = !switchColor;
prevRAName = currRAName;
}
} else {
prevRAName = currRAName; // hits on the first row ...
}
return switchColor;
}
在模板中我有:
<tr ng-repeat="item in productusage | orderBy : ['RAName','PeriodStart']">
<td ng-style="alternate(item.RAName) ? {'background-color':'#f5f5f5'} : {'background-color':'white'}">{{item.RAName}}</td>
<td ng-style="alternate(item.RAName) ? {'background-color':'#f5f5f5'} : {'background-color':'white'}">{{item.ProductDescription}}</td>
<td ng-style="{'background-color': '#' + intToRGB(hashCode(item.PeriodStart)), 'color' : 'white'}">{{item.PeriodCaseCt}}</td>
<td ng-style="{'background-color': '#' + intToRGB(hashCode(item.PeriodStart)), 'color' : 'white'}">{{item.PeriodStart | date : format : shortDate}}</td>
</tr>
我想交替表的 RAName 和 ProductDescription 字段的颜色,根据 RAName 有效地“分组”它们。PeriodCaseCt 和 PeriodCaseCt 字段采用颜色编码。现在一切正常,我希望这是故事的结局,但是我收到了一堆这些错误:
Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting!
Watchers fired in the last 5 iterations: [[{"msg":"alternate(item.RAName) ? {'background-color':'#f5f5f5'} : {'background-color':'white'}","newVal":{"background-color":"white"},"oldVal":{"background-color":"#f5f5f5"}}
现在,我不需要在第一次通过后观察这些值,它确实正确设置了所有内容,并且最终用户永远不会知道,除非他们打开了开发人员工具/控制台选项卡,但我知道它会窃听 $# %% 出我。我该如何改进/解决这个问题?