2015-10-18 93 views
1

我有以下ng-repeatAngularJS在NG-重复序列

<div ng-repeat="product in ProductList" ng-show="ProductList.length >=1"> 
<h3> Product {{ProductList.length + 1}}</h3> 

所有我需要做的是展现在h3序号例如

Product 1 
Product 2 
.... 

我该怎么做呢?

回答

1

您可以使用$index,看到doc

<div ng-repeat="product in ProductList"> 
    <h3>Product {{$index + 1}}</h3> 
</div> 

你不需要ngShow因为ngRepeat不会重复一个空数组

0

我不知道,如果你只是想productProductList(即ProductList.indexOf(product) + 1)的位置/顺序,或一些独特的标识符存储在product变量。这里是你会怎么做都:

<h3> Product {{$index + 1}}</h3>

<h3> Product {{product.serial}}</h3>


为了解决一个评论者所关注的问题,如果你有一些复杂的范围的问题,比如嵌套ng-repeat(你没有,所以我认为你没有),最明确的方式来表达意图将是:

<div ng-repeat="product in ProductList" ng-init="serial = $index"> 
... 
<h3> Product {{serial}}</h3> 

但我不会引入另一个变量(即, serial),直到你需要。 $index将成为大多数人所期望的ng-repeat,它不会在内部做一些疯狂的范围创建。

+0

有时$指数将使prolem是另一个范畴注入withing NG-重复 –

+0

我不认为'ng-show =“ProductList.length> = 1”'是需要的.' $ index'也可以工作... – Rayon