在没有 ng-if 的情况下从 ng-repeat 中排除嵌套数组中的元素
Exclude elements in a nested array from ng-repeat without ng-if
给定一个嵌套数组,我如何在 ng-repeat 中过滤这些值而不必使用 ng-if。
不使用 ng-if 的原因是我需要零值才能不占用 $index,因为我需要将该索引与该视图中发生的其他事情相匹配。例如,这对我的 atm 不起作用:
<tr ng-if="row.value[0] !== 0 || row.value[1] !== 0" ng-repeat="row in chart.values track by $index">
在下图中,例如,如果 value[0] 或 value[1] 在 Asian/Indian 下为零,则它们不应出现在 ng-repeat 中。数据如下所示:
快速建议:在生成数组的位置执行业务逻辑,而不是在 ng-repeat 中执行
类似于此处,但不更改数据源,而是进行比较:angular.js ng-repeat - check if conditional is true then use another collection
您可以使用 ng-show
并在方法中求值;这将保留 $index
'es :
$scope.showRow = function(row) {
return row.value[0] !== 0 || row.value[1]!== 0
}
<tr ng-repeat="row in chart.values track by $index" ng-show="showRow(row)">
更新。如果您不想保留 $index
,您可以将上面的 showRow
修改为:
$scope.showRow = function(row) {
return !(row.value[0] == 0 || row.value[1] == 0)
}
然后将其用作 filter
:
<tr ng-repeat="row in chart.values | filter:showRow track by $index">
因此,无论过滤掉什么,跟踪的 $index
将是 0、1、2、3、4...等。
给定一个嵌套数组,我如何在 ng-repeat 中过滤这些值而不必使用 ng-if。
不使用 ng-if 的原因是我需要零值才能不占用 $index,因为我需要将该索引与该视图中发生的其他事情相匹配。例如,这对我的 atm 不起作用:
<tr ng-if="row.value[0] !== 0 || row.value[1] !== 0" ng-repeat="row in chart.values track by $index">
在下图中,例如,如果 value[0] 或 value[1] 在 Asian/Indian 下为零,则它们不应出现在 ng-repeat 中。数据如下所示:
快速建议:在生成数组的位置执行业务逻辑,而不是在 ng-repeat 中执行
类似于此处,但不更改数据源,而是进行比较:angular.js ng-repeat - check if conditional is true then use another collection
您可以使用 ng-show
并在方法中求值;这将保留 $index
'es :
$scope.showRow = function(row) {
return row.value[0] !== 0 || row.value[1]!== 0
}
<tr ng-repeat="row in chart.values track by $index" ng-show="showRow(row)">
更新。如果您不想保留 $index
,您可以将上面的 showRow
修改为:
$scope.showRow = function(row) {
return !(row.value[0] == 0 || row.value[1] == 0)
}
然后将其用作 filter
:
<tr ng-repeat="row in chart.values | filter:showRow track by $index">
因此,无论过滤掉什么,跟踪的 $index
将是 0、1、2、3、4...等。