增加ng-repeat中的变量

时间:2016-04-16 04:34:33

标签: javascript angularjs

您好我是角度js的新手。我想像这样计算ng-repeat:

<div ng-repeat="people in peoples">
{{ numberPeople++ }}. {{people.name}}
<div ng-repeat="repeople in people.families">
{{ numberPeople++ }}. {{repeople.name}}
</div>
</div>
<div ng-repeat="otherpeople in otherpeoples">
{{ numberPeople++ }}. {{otherpeople.name}}
</div>
{{ numberPeople }}
</div>

numberPepple将在每次ng-repeat中增加 我看到很多问题,他们说使用$ index但我使用了很多ng-repeat

3 个答案:

答案 0 :(得分:1)

您可以使用 ng-init 。 Tyr:

  <div ng-repeat="people in peoples" ng-init="numberPeople=numberPeople+1">
     {{ numberPeople }}. {{people.name}}
     <div ng-repeat="repeople in people.families" ng-init="numberPeople=numberPeople+1">
       {{ numberPeople }}. {{repeople.name}}
      </div>
      </div>
       <div ng-repeat="otherpeople in otherpeoples" ng-init="numberPeople=numberPeople+1">
        {{ numberPeople }}. {{otherpeople.name}}
         </div>
        {{ numberPeople }}
        </div>

答案 1 :(得分:0)

您可以在JS文件中使用angular.forEach并在模型中进行设置。在HTML代码中使用该模型。

答案 2 :(得分:0)

我猜测@models = Port.select(:id, :device_id, :number, :value, :sensor, :UOM).all.order('number asc').group(:id,:sensor,:UOM) 无法正常工作,在您的控制器中添加++顶部,它会将范围变量初始化为整数类型并为其指定$scope.numberPeople = 0;。然后,您可以使用0