如何根据子阵列长度过滤angularJS中的数组

时间:2015-11-22 11:52:19

标签: angularjs html5

我有这样的模型

   public class City
   {
     public string Name {get; set;}
     public List<Classified> Classifieds {get; set;}
     public List<Area> Areas {get; set;}
   }

在我的angualrJs控制器中,我喜欢下面的

$scope.cities = data; // I'm getting data (array of city).

现在,我正在根据citiesClassifieds(.length > 0)尝试过滤Areas (.length > 0),如下所示。

<tr ng-repeat="city in cities | filter: { classifieds: '!!' }">    -- Not Working.

<tr ng-repeat="city in cities | filter: { areas: '!!' }">    -- Not Working.

我想要一些像这样的东西

<tr ng-repeat="city in cities | filter: { areas.length >0 }">    -- Tried this one as well. Not Working.

1 个答案:

答案 0 :(得分:0)

如果您的$scope.cities变量包含以下格式的数据:

$scope.cities = [{name: 'a', classifieds: [], areas: []}, 
                 {name: 'b', classifieds: [], areas: [1]}, 
                 {name:'c', classifieds:[1], areas:[]}];

您可以创建自定义过滤器功能,例如:

$scope.hasData = function(city) {
    return city.areas.length > 0 || city.classifieds.length > 0;
};

用法:

  <td ng-repeat="city in cities | filter: hasData">{{city.name}}</td>

编辑:

JSFiddle:https://jsfiddle.net/9sxeawr5/