如何正确添加"跟踪"到ngRepeat

时间:2016-11-16 16:18:07

标签: angularjs

我有这个HTML

<ion-item ng-repeat="item in ::itemsToShow = extensionItems | orderBy:'distance' " class="entry" ng-class="{'alt-background': $index % 2 == 1}" ng-click="itemDetail(item)">
    ...details...
</ion-item>

我无法找出添加track by:item._id的正确方法我总是收到此错误:

  

语法错误:令牌跟踪&#39;是表达式第30列的意外标记[itemsToShow = extensionItems track by:_id | ...

我试过了:

<ion-item ng-repeat="item in ::itemsToShow = extensionItems track by:item._id | orderBy:'distance' " class="entry" ng-class="{'alt-background': $index % 2 == 1}" ng-click="itemDetail(item)">

<ion-item ng-repeat="item in ::itemsToShow = extensionItems track by:_id | orderBy:'distance' " class="entry" ng-class="{'alt-background': $index % 2 == 1}" ng-click="itemDetail(item)">

<ion-item ng-repeat="item in ::itemsToShow = extensionItems  | orderBy:'distance' track by:_id" class="entry" ng-class="{'alt-background': $index % 2 == 1}" ng-click="itemDetail(item)">

<ion-item ng-repeat="item in ::itemsToShow track by:_id = extensionItems  | orderBy:'distance' " class="entry" ng-class="{'alt-background': $index % 2 == 1}" ng-click="itemDetail(item)">

还尝试使用_iditem._id并且我总是得到相同的错误(当然是不同的列)。我应该如何或在哪里写track by

修改:Angular版本1.4.3

编辑2 :Json数据具有此结构

_id: "000000426"
_rev: "1-5003008fcf25b8f130233b944bb761c9"
someText: "<p class="bodytext ">Something for you.</p>"
name : "You-Shop"
homepage : "http://www.youshop.com"
id: 426

2 个答案:

答案 0 :(得分:0)

您错过了track by必须后跟冒号(:)...

的事实

答案 1 :(得分:0)

`track by` MUST be the final statement in a ng-repeat.