2016-08-03 62 views
0

我有运营商的一个这样的数组:角度滤波不显示物体与空属性

[ 
    { 
     "id":1, 
     "key":"55330", 
     "name":"Macie", 
     "operative_status":{ 
     "id":20, 
     "code":"100", 
     "name_status":"viaje", 
     "created_at":"2016-08-03T21:28:52Z" 
     } 
    }, 
    { 
     "id":2, 
     "key":"8439", 
     "name":"Darian", 
     "operative_status":null 
    }, 
    { 
     "id":3, 
     "key":"49531", 
     "name":"Kaelyn", 
     "operative_status":null 
    } 
] 

而且我想用显示它们的ng-repeat

<div ng-repeat="operator in operators | filter:{ operative_status : {name_status: status}}"> 

正如你所看到的,我想按状态过滤。

问题是它没有显示具有null的操作符的操作符。

+0

什么是'name_status'? – developer033

+0

一个属性operative_status –

回答

1

要过滤你必须做一些null值像这样的事情:

<div ng-repeat="operator in operators | filter: { operative_status : search && {name_status: search } || '!'}"> 

<div ng-repeat="operator in operators | filter: { operative_status : search ? {name_status: search } : '!'}"> 

这里的工作例如

(function() { 
 
    "use strict"; 
 

 
    angular 
 
    .module('app', []) 
 
    .controller('MainCtrl', MainCtrl); 
 

 
    MainCtrl.$inject = ['$scope']; 
 

 
    function MainCtrl($scope) { 
 
    $scope.operators = [ 
 
     { 
 
      "id":1, 
 
      "key":"55330", 
 
      "name":"Macie", 
 
      "operative_status":{ 
 
      "id":20, 
 
      "code":"100", 
 
      "name_status":"viaje", 
 
      "created_at":"2016-08-03T21:28:52Z" 
 
      } 
 
     }, 
 
     { 
 
      "id":2, 
 
      "key":"8439", 
 
      "name":"Darian", 
 
      "operative_status":null 
 
     }, 
 
     { 
 
      "id":3, 
 
      "key":"49531", 
 
      "name":"Kaelyn", 
 
      "operative_status":null 
 
     } 
 
    ]; 
 
    } 
 
})();
<!DOCTYPE html> 
 
<html ng-app="app"> 
 

 
<head> 
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.min.js"></script> 
 
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.min.css" /> 
 
</head> 
 

 
<body ng-controller="MainCtrl"> 
 
    <input type="text" class="form-control" ng-model="search" placeholder="Search..."> 
 
    <hr> 
 
    <div ng-repeat="operator in operators | filter: { operative_status : search && {name_status: search } || '!'}"> 
 
    <pre ng-bind="operator | json"></pre> 
 
    </div> 
 

 
</body> 
 

 
</html>

+1

谢谢,它工作 –

1

对于您的数据,将模式对象用作过滤器表达式将不起作用,因为operative_status属性的值为null的项目将永远不匹配,这就是您正在观察的内容。

您可以使用谓词函数来代替过滤器表达式。

例如:

$scope.filterByStatus = function (value) { if ($scope.status.length === 0) return true; return value.operative_status && value.operative_status.name_status === $scope.status; };

,然后用它在你的过滤器表达式:

operator in operators | filter:filterByStatus

Demonstration Plunker

+0

谢谢,这个工程,如果我改变这一行如果($ scope.status.length === 0)返回true;到这个if($ scope.status === undefined)。 –