我的要求是在angularj中每次迭代ng repeat的$ index值。但是它不起作用,并且在NG-Repeat的开始时会增加。有人可以帮助我吗?
<div class ="row" ng-repeat ="offer in offers track by $index" ng-init="$index=$index+5">
<div class="col-md-6">
<span>offers[$index].title</span>
<span>offers[$index+1].title</span></div>
<div class="col-md-4"><span>offers[$index+2].title</span>
<span>offers[$index+3].title</span>
<span>offers[$index+4].title</span></div>
</div>
我使用的一排,替代在第一行中显示两个标题,在第二行中,它显示了其他三个报价的标题。第一个迭代按顺序打印标题。但是在第二个迭代中,它重复了数据由于$ index值为2。
angularjs为每个迭代提供索引值,您可以使用索引值并更改计数。
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body ng-app="myApp" ng-controller="myCtrl">
Count = {{count}}
<h1 ng-repeat="x in records track by $index">{{x}} {{$index}} Count : {{count+$index}}</h1>
<script>
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
$scope.count = 5;
$scope.records = [
"Iteration : ",
"Iteration : ",
"Iteration : ",
"Iteration : ",
]
});
</script>
</body>
</html>
chech the angularjs doc link
尝试以下内容:
var myApp = angular.module('myApp', []);
myApp.controller('myController', ['$scope', function($scope){
$scope.offers = [{title: 'Title 1'},{title: 'Title 2'},{title: 'Title 3'}];
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app='myApp' ng-controller='myController'>
<div ng-repeat ="offer in offers">
<span>{{offers[$index + 0].title}}</span>
</div>
</div>