我有这两个循环
<ion-list>
<ion-item ng-repeat="category in categories" href="#">
{{category.name}} {{category.id}}
<ion-item ng-repeat="test in tests" href="#">
{{test.name}} {{test.cat_id}}
</ion-item>
</ion-item>
</ion-list>
我想测试 if(category.id === test.cat_id)
然后显示第二个列表
我不确定是否以及如何使用 ngif
,或者我是否应该在 Controller 中处理这个问题并返回一个已根据我的需要格式化的对象..
有什么想法吗?
最佳答案
如果我理解正确,你想有条件地显示第二个 ng-repeat。如果是这种情况,那么您可以使用它:
<ion-list>
<ion-item ng-repeat="category in categories" href="#">
{{category.name}} {{category.id}}
<ion-item ng-repeat="test in tests" href="#" ng-if="category.id === test.cat_id">
{{test.name}} {{test.cat_id}}
</ion-item>
</ion-item>
</ion-list>
另一种选择是使用这样的过滤器:
<ion-list>
<ion-item ng-repeat="category in categories" href="#">
{{category.name}} {{category.id}}
<ion-item ng-repeat="test in tests | filter: {cat_id: category.id}" href="#" ng-if="category.id === test.cat_id">
{{test.name}} {{test.cat_id}}
</ion-item>
</ion-item>
</ion-list>
我实际上会推荐过滤器选项。
关于javascript - 如何处理 AngularJS 循环中的 if 语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23303628/