2017-12-27 636 views
0

我的JSON数据看起来像下面的代码。你可以帮助我通过代码/ desc过滤数据,这是在状态属性。针对JSON键/值嵌套对象的角度js过滤器对我无效

$scope.agent = 
{ 
    "0d297c1711de": 
    [{ 
     "applicationName": "rewards-accounts", "agentId": "0d297c1711de", 
     "status": { "agentId": "0d297c1711de", "eventTimestamp": 1510580172247, "state": { "code": 100, "desc": "Running" } } 
    }], 
    "16f279d66923": 
    [{ 
     "applicationName": "rewards-accounts-details", "agentId": "16f279d66923", 
     "status": { "agentId": "0d297c1711de", "eventTimestamp": 1510580172247, "state": { "code": 201, "desc": "Unexpected Shutdown" } } 
    }], 
    "203b353d32ef": 
    [{ 
     "applicationName": "rewards-accounts-details", "agentId": "203b353d32ef", 
     "status": { "agentId": "0d297c1711de", "eventTimestamp": 1510580172247, "state": { "code": 200, "desc": "Shutdown" } } 
    }] 
}; 

我在ng-repeat中使用了这个过滤器。它不工作。 selectedCode是我的模型数据进行过滤。

| filter:{status:{ state: { code: **selectedCode**}}} 
+0

是你的对象被渲染,但没有得到过滤,或者它不呈现nd给你'错误:[filter:notarray]'? –

回答

0

正如this SO

说你需要在参数传递由,这在你的具体情况看起来像过滤:

ng-repeat="a in agent | filter: {status: {state: {code: filter.key}}}" 

与感谢@Ray

JSFiddle here作进一步参考。

希望它有帮助。

0

您可以使用自定义过滤器来过滤你的嵌套的数据,

在你的控制器创建过滤器

$scope.customFilter = function (row) { 
    var result = false; 
    if($scope.filter == undefined || $scope.filter == ""){ 
      result = true; 
    } 
    else{ 
     angular.forEach(row, function(value, key){ 
      if(value.state != undefined){ 
       if(value.state.code == $scope.filter) 
       result = true; 
      } 
     }); 
    } 
    return result; 
}; 

和这个过滤器添加到您的NG-重复

<div ng-repeat="item in agent"> 
    <div ng-repeat="x in item | filter:customFilter"> 
    {{x}} 
    </div> 
</div> 

Demo

+0

它不工作。我没有使用控制器。我正在使用指令,我在我的指令中添加了自定义过滤器,并且它不工作。谢谢。 –