angularjs how to increment init variable value in ng-repeat without onclick?

我与影子孤独终老i 提交于 2019-12-19 06:28:25

问题


How to increment init variable value 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 increment start=start+1 in ng-repeat but no count only show 1.


回答1:


This is because each ng-repeat has its own scope and each ng-init will just increase its own scoped variable. You can try access the parent scope with $parent.

In case you are looking for a continuous numeration 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 the +3 should be +[LENGTH OF INNER ARRAY].

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

For your specific code it would look like this

<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>



回答2:


I guess you achieve this by simple {{$parent.$index}}

Update

As per 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




回答3:


You can be accessed 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' } ];
}



回答4:


AngularJS is giving $index variable. We can use it.     
<ul>
 <li ng-repeat="item in items">{{ $index + 1 }}</li>
</ul>


来源:https://stackoverflow.com/questions/29297301/angularjs-how-to-increment-init-variable-value-in-ng-repeat-without-onclick

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!