如何在嵌套的ng-repeat指令中使用控制语句(if语句)?

时间:2015-06-30 15:24:41

标签: javascript jquery angularjs

我的HTML视图中有以下代码:



<div ng-repeat='shop in shops'>
  <ul ng-repeat='item in items'>
    <li>
      <!-- {{Show items of the particular shop in here}} -->
    </li>
  </ul>
</div>
&#13;
&#13;
&#13;

items是一个JSON对象。每个项目都有一个shop_id,必须用它来进行排序。

根据文档,我知道我不能在Angular Expression中使用if控制语句来实现我的排序。我怎么能使用控制语句。

我试过了,但显然失败了,因为我们无法返回continue

&#13;
&#13;
//var resources.items and resources.shops are JSON objects output from a database

//This file is app.js

$scope.itemName = function(itemIndex, shopIndex) {
  for (var i = 0; i < resources.items.length; i++) {
    if (resources.items[itemIndex].shop_id == resources.shops[shopIndex].id)
      return resources.items[itemIndex].name;
  };
  return continue;
}
&#13;
&#13;
&#13;

并且,在视图中:

&#13;
&#13;
<div class="tab-content" ng-repeat='provider in providers'>
  <ul ng-repeat='item in items'>
    <li>{{itemName($index, $parent)}}</li>
  </ul>
</div>
&#13;
&#13;
&#13;

我是Angular的初学者。请帮忙。

1 个答案:

答案 0 :(得分:3)

使用ngIf指令:

<div ng-repeat='shop in shops'>
  <ul ng-repeat='item in items'>
    <li ng-if="item.shop_id === shop.id">
      <!-- {{Show items of the particular shop in here}} -->
    </li>
  </ul>
</div>