3

我有一个数组,我根据选择的类别打印出它的元素

<li class="thumbnail" ng-repeat="temp in templates| filter:{temp.category_id :       selectedCategoryId }:true">

模板看起来像这样:

[{"id":"1","account_id":null,"category_id":"1","name":"Blue","thumbnail":"placehold.it/210x300","category_name":"test"},{"id":"2","account_id":null,"category_id":"1","name":"Red","thumbnail":"placehold.it/210x300","category_name":"test"},{"id":"3","account_id":null,"category_id":"2","name":"Green","thumbnail":"placehold.it/210x300","category_name":"test again"}]

谢谢您的帮助 !

编辑:我还想添加一个额外的条件,即 temp.category_id == null ,全部显示。

4

2 回答 2

2

您无需在过滤器中再次指定 temp。

改变

 filter:{temp.category_id : selectedCategoryId }:true

 filter:{category_id : selectedCategoryId }:true

这是它的工作原理:http: //jsfiddle.net/Sqq3s/

于 2013-11-08T22:38:01.857 回答
0

您的过滤器应该是:

<li ng-repeat="temp in templates | filter:{category_id : selectedCategoryId}:true"> ... </li>

写作filter: {temp.category_id : selectedCategoryId}会在对象中寻找属性temp.category_id(这是一个无效的属性名称,它应该读取'temp.category_id'temp。但是,您要查找该属性category_id

于 2013-11-08T22:35:50.720 回答