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

标签 javascript jquery angularjs

我的 HTML View 中有以下代码:

<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 表达式中使用 if 控制语句来实现我的排序。我还能如何使用控制语句。

我尝试了这个,但显然失败了,因为我们无法返回继续

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

并且,在 View 中:

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

关于javascript - 如何在嵌套的 ng-repeat 指令中使用控制语句(if 语句)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31142372/

相关文章:

javascript - 未找到 Yeoman Angular 特征生成器 cdnify 任务

Android 的 WebView 中的 JavascriptInterface : multiple calls to JS cause deadlock

javascript - 在第二个选择中获取空白选项取决于第一个选择选项 - AngularJS

angularjs - Angular 自举崩溃的奇怪行为

javascript - 在不刷新页面的情况下重新加载图像

具有以字符串开头的属性的元素的 jQuery 选择器

javascript - 使用 angularjs 对 Json 文件进行 Ng 重复?

Javascript 将表行放置在 ID 较大和较小的行之间

php - 更新多个页面

JavaScript 倒计时器代码 [JS]