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

时间:2015-03-27 09:30:18

标签: angularjs angularjs-ng-repeat

如何在ng-repeat中增加init变量值

   <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在增量start = start + 1 in ng-repeat但没有计数只显示1.

4 个答案:

答案 0 :(得分:13)

这是因为每个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>

答案 1 :(得分:6)

我猜你是通过简单的{{$ parent。$ index}}

实现的

<强>更新

根据评论

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

控制器: -

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

Fiddle

答案 2 :(得分:3)

您可以使用$index进行访问,请查看此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>

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

答案 3 :(得分:2)

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