在ng-repeat中每次迭代后修改$ index值

时间:2017-03-15 07:41:03

标签: javascript angularjs

我的要求是在angularjs.每次迭代ng-repeat后增加$ index值。我尝试使用像这样的ng-init递增, 但它没有起作用,并且在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>
&#13;
&#13;
&#13;

问题我使用一行,在第一行和第二行中交替显示两个标题,它显示其他三个商品的标题。第一次迭代按顺序打印标题。但在第二次迭代中,它复制数据因为$ index值是2.所以我需要操作$ index值,以便它按顺序显示标题。

2 个答案:

答案 0 :(得分:1)

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 That AngularJs Doc Link

答案 1 :(得分:1)

尝试以下方法:

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>