一尘不染

angularjs如何在没有onclick的情况下增加ng-repeat中的初始变量值?

angularjs

如何增加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-repeat中递增start = start + 1之后ng-init start = 0,但没有计数仅显示1。


阅读 213

收藏
2020-07-04

共1个答案

一尘不染

这是因为每个人ng-repeat都有自己的作用域,并且每个ng-init人只会增加自己的作用域变量。您可以尝试使用访问父作用域$parent

如果您正在寻找连续的计数,请尝试此

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

+3应该在哪里+[LENGTH OF INNER ARRAY]

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

对于您的特定代码,它看起来像这样

<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>
2020-07-04