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