如何在每次 ng-repeat 调用时将 $Index 设置为 1
how to set $Index to 1 every ng-repeat call
<table class="table table-bordered">
<tr ng-repeat="x in AppliedJob">
<td>{{x.JobName}}</td>
<td> <span class="dropdown" ng-repeat="y in AppliedCenter| unique: 'Location' " ng-if="y.JobName==x.JobName">
{{$index+1}}){{y.Location}}
</span> </td></tr> </table>
以上代码的输出
1) Tester : 1)India 2)USA 3)Australia
2) Developer : 4)Japan 5)China
需要输出
1) Tester : 1)India 2)USA 3)Australia
2) Developer : 1)Japan 2)China
我想将 $Index
设置为 1
。
发生这种情况是因为您正在使用 ng-if
进行过滤,这不会影响数组及其索引,它只是从 DOM 中删除元素。您应该在 ng-if
中使用 filter 而不是 y.JobName==x.JobName
比较,因此您的 ng-repeat
表达式应该像这样 y in AppliedCenter | unique: 'Location' | filter: { JobName: x.JobName }
。参见示例:
angular
.module('Test', [])
.controller('TestController', ['$scope', function($scope) {
$scope.cities = ["New York", "LA"];
$scope.jobs = [{ name: "QA", city: "New York"}, { name: "Frontend", city: "New York"}, {name: "Backend", city: "New York"}, {name: "DBA", city: "LA"}, { name: "QA", city: "LA"}];
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<div ng-app="Test">
<div ng-controller="TestController">
<table>
<tr ng-repeat="city in cities">
<td ng-bind="city"></td>
<td>
<span ng-repeat="job in jobs | filter:{city: city}">{{($index + 1) + ")" + job.name}} </span>
</td>
</tr>
</table>
</div>
</div>
<table class="table table-bordered">
<tr ng-repeat="x in AppliedJob">
<td>{{x.JobName}}</td>
<td> <span class="dropdown" ng-repeat="y in AppliedCenter| unique: 'Location' " ng-if="y.JobName==x.JobName">
{{$index+1}}){{y.Location}}
</span> </td></tr> </table>
以上代码的输出
1) Tester : 1)India 2)USA 3)Australia
2) Developer : 4)Japan 5)China
需要输出
1) Tester : 1)India 2)USA 3)Australia
2) Developer : 1)Japan 2)China
我想将 $Index
设置为 1
。
发生这种情况是因为您正在使用 ng-if
进行过滤,这不会影响数组及其索引,它只是从 DOM 中删除元素。您应该在 ng-if
中使用 filter 而不是 y.JobName==x.JobName
比较,因此您的 ng-repeat
表达式应该像这样 y in AppliedCenter | unique: 'Location' | filter: { JobName: x.JobName }
。参见示例:
angular
.module('Test', [])
.controller('TestController', ['$scope', function($scope) {
$scope.cities = ["New York", "LA"];
$scope.jobs = [{ name: "QA", city: "New York"}, { name: "Frontend", city: "New York"}, {name: "Backend", city: "New York"}, {name: "DBA", city: "LA"}, { name: "QA", city: "LA"}];
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<div ng-app="Test">
<div ng-controller="TestController">
<table>
<tr ng-repeat="city in cities">
<td ng-bind="city"></td>
<td>
<span ng-repeat="job in jobs | filter:{city: city}">{{($index + 1) + ")" + job.name}} </span>
</td>
</tr>
</table>
</div>
</div>