如何在嵌套ng重复指令中使用控制语句(if语句)

How to use a control statement (if statement) in a nested ng-repeat directive?

本文关键字:控制语句 if 语句 指令 嵌套 ng      更新时间:2023-09-26

我的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>

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

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

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

//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;
}

而且,在这个观点中:

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

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

使用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>