2016-11-17 159 views
1

我從api調用了一個json數據來調用包含id,name,error_type和message的各種項目的錯誤列表。我需要顯示一個包含該項目的名稱和標識的標題,並在該標識下包含所有錯誤的列表。我正在使用獨特的過濾器角度,但是跳過具有相同ID的下一個對象。當id相同時我需要一些幫助來合併對象屬性。使用js數據將具有相同id的重複對象合併到json數據中的單個對象中使用angular js

var items =[ {  
 
    "id": 83739, 
 
    "name": "abcd", 
 
    "type": "error", 
 
    "msg": "For Macros, x >= y" 
 
}, 
 
{ 
 
    "id": 83739, 
 
    "name": "abcd", 
 
    "type": "warning", 
 
    "msg": "Missing Power Condition" 
 
},{ 
 
    "id": 83740, 
 
    "name": "efgh", 
 
    "type": "error", 
 
    "msg": "Missing Power supply" 
 
}]
<div ng-repeat="item in items | unique:'id'"> 
 
<h4><a href="#/product/item/{{item.id}}"> {{io.item}}</a></h4> 
 
<ul> 
 
    <li > {{item.type}}: {{item.msg}}</li> 
 
</ul> 
 
</div>

我輸出我需要的是這樣的

<div> 
 
    <h4><a href="#/product/item/83739"> abcd</a></h4> 
 
    <ul> 
 
    <li > error: For Macros, x >= y</li> 
 
    <li > warning: Missing Power Condition</li> 
 
    
 
    </ul> 
 
</div> 
 

 
<div> 
 
    <h4><a href="#/product/item/83740}"> efgh</a></h4> 
 
    <ul> 
 
    <li > error: Missing Power supply</li> 
 
    </ul> 
 
</div>

回答

1

添加NG-重複指令貴麗和應用過濾器,像這樣:

<li ng-repeat="i in items | filter: {id: item.id}"> {{i.type}}: {{i.msg}}</li> 

在背景:

<div ng-repeat="item in items | unique:'id'"> 
    <h4> 
     <a href="#/product/item/{{item.id}}"> {{io.item}}</a> 
    </h4> 
    <ul> 
     <li ng-repeat="i in items | filter: {id: item.id}"> {{i.type}}: {{i.msg}}</li> 
    </ul> 
</div> 

這裏的工作plunk

+0

非常感謝。有效。 –

相關問題