Angular1过滤器:{field:' !!'}无效

时间:2016-11-23 11:19:46

标签: angularjs filter

我想要列表中最基本的过滤器,如果某个值为null(而不是数组),则将其过滤掉

JSON示例

{
  "runningServices": [
    {
      "servicename":"service1",
      "previousLocations": null,
      "cancelReason": null,
      "delayReason": null
    },
    {
      "servicename":"service2"
      "previousLocations": null,
      "cancelReason": null,
      "delayReason": {
         "reason":"pigeons"
      }
    },

    ]
}

我的代码(控制器将数据绑定到$ scope.listOfServices = data.runningServices)

<tr ng-repeat="service in listOfServices | filter:{delayReason:'!!'}>
//stuff
</tr>

没有过滤器,所有内容都会列出,过滤器我只是得到一个空白的屏幕..也尝试了&#39;!&#39;

1 个答案:

答案 0 :(得分:2)

编写一个小过滤器(在我的示例中为 nullFilter ):

app.controller('MainCtrl', function($scope) {
  $scope.name = 'World';

  $scope.listOfServices = {
    "runningServices": [
      {
        "servicename":"service1",
        "previousLocations": null,
        "cancelReason": null,
        "delayReason": null
      },
      {
        "servicename": "service2",
        "previousLocations": null,
        "cancelReason": null,
        "delayReason": {
           "reason": "pigeons"
        }
      }
    ]
  }

  $scope.nullFilter = function(value){
    return value.delayReason !== null;
  };
});

并在HTML中:

<div ng-repeat="service in listOfServices.runningServices | filter: nullFilter">
  {{service.servicename }}
</div>

plunker:https://plnkr.co/edit/pc2TkQh5qlnoF6JqfqZh?p=preview