Angularjs how to increase value of initialization variable in ng-repeat without onclick?

How to increase value of initialization variable in ng-repeat

<div ng-if="feedData.carouselMode == true" ng-init='start=0' ng-repeat="a in carousel_data"> <div class="owl-demo" class="owl-carousel" owl-carousel > <div class="item" ng-repeat="(key, item) in a.carouselFeeds" ng-init="start=start+1"> {{start}} <div ng-click="go('{{key}}', item.feedUrls.length, 'rtl')" ng-swipe-left="go('{{key}}', item.feedUrls.length, 'rtl')"> <img class="lazyOwl" ng-src="{{item.img}}" /> <span class="innersquare" image-ja="{{item.img}}"> <p ng-style="folderStyle">{{item.name }}&nbsp;</p> </span> </div> </div> </div> </div> 

ng-init start = 0 after the start of the increment = start + 1 in the ng-repeat, but the count does not show only 1.

+7
angularjs angularjs-ng-repeat
source share
3 answers

This is because each ng-repeat has its own scope, and each ng-init will simply increment its own variable with scope. You can try to access the parent area with $parent .

If you are looking for continuous numbering, try this.

 <div ng-controller="MyCtrl"> <div ng-repeat="a in ['a','b','c']" ng-init="current = $parent.start; $parent.start=$parent.start+3;"> <div ng-repeat="x in ['alpha','beta','gamma']"> {{current + $index}} </div> </div> 

Where +3 should be +[LENGTH OF INNER ARRAY] .

http://jsfiddle.net/rvgvs2jt/2/

For your specific code, it will look like

 <div ng-if="feedData.carouselMode == true" ng-init='current=$parent.start; $parent.start=$parent.start+a.carouselFeeds.length' ng-repeat="a in carousel_data"> <div class="owl-demo" class="owl-carousel" owl-carousel> <div class="item" ng-repeat="(key, item) in a.carouselFeeds"> {{current + $index}} <div ng-click="go('{{key}}', item.feedUrls.length, 'rtl')" ng-swipe-left="go('{{key}}', item.feedUrls.length, 'rtl')"> <img class="lazyOwl" ng-src="{{item.img}}" /> <span class="innersquare" image-ja="{{item.img}}"> <p ng-style="folderStyle">{{item.name }}&nbsp;</p> </span> </div> </div> </div> </div> 
+13
source share

I think you achieve this with simple {{$ parent. $ index}}

Update

According to comments

 <div ng-controller="MyCtrl"> <div ng-repeat="a in one"> <div ng-repeat="x in two"> {{($parent.$index*two.length)+($index+1)}} </div> </div> 

Controller: -

 $scope.one= ['alpha','beta','gamma'] ; $scope.two=['alpha','beta','gamma']; 

Fiddle

+5
source share

You can be reached with $ index , check this code ,

 <div ng-app ng-controller="TestController"> <div ng-repeat="item in list"> <label>Input {{$index+1}}:</label> <input ng-model="item.value" type="text"/> </div> {{list}} </div> 

and

 function TestController($scope) { $scope.list = [ { value: 'value 1' }, { value: 'value 2' }, { value: 'value 3' } ]; } 
+2
source share

All Articles