2017-12-27 638 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

它不工作。我沒有使用控制器。我正在使用指令,我在我的指令中添加了自定義過濾器,並且它不工作。謝謝。 –