我有这个ng-repeat
<tr ng-repeat="website in websites"> <td>{{website.url}}</td> </tr>
来自网站数组的每个网站对象如下所示:
{ url: "example.com",groups: [] }
问题:如何将过滤器应用于上面的循环,以便它只显示groups属性为空数组的元素?
我试过的事情:
data-ng-repeat="website in websites | filter: {groups: []}" data-ng-repeat="website in websites | filter: !groups.length" data-ng-repeat="website in websites | filter: groups.length === 0"
data-ng-repeat="website in websites | filter: {groups: ''}"
(与我想要的相反,并且仅显示组不是空数组的项目)
data-ng-repeat="website in websites | filter: {groups: null}"
(如果不使用[]我使用null来表示没有值,这可行,但它看起来非常混乱…因为我需要经常注意group属性变空,并手动将其设置为null)
我在控制器中添加了一个过滤功能:
原文链接:https://www.f2er.com/angularjs/240591.htmlJS:
angular.module('myApp',[]) .controller('myController',['$scope',function($scope) { $scope.friends = [{ name: 'John',phone: '555-1276',a: [1,2,3] },{ name: 'Mary',phone: '800-BIG-MARY',{ name: 'Mike',phone: '555-4321',a: null },{ name: 'Adam',phone: '555-5678',a: [] },{ name: 'Julie',phone: '555-8765',{ name: 'Juliette',a: [] }]; $scope.filterFn = function(item) { // must have array,and array must be empty return item.a && item.a.length === 0; }; } ]);
在您的模板中:
<table> <tr><th>Name</th><th>Phone</th><th>array len</th></tr> <tr ng-repeat="friend in friends | filter: filterFn"> <td>{{friend.name}}</td> <td>{{friend.phone}}</td> <td>{{friend.a.length}}</td> </tr> </table>