2015-07-20 54 views
0

我也想用ng-repeat顯示鍵「scott」和「bill」及其所有屬性值(全名和年齡)。你會如何重複這個

{ 
    "attributes": { 
     "scott": { 
      "fullname": "Scott D Man", 
      "age": "string" 
     }, 
     "bill": { 
      "fullname": "William Shatner", 
      "age": "string" 
     } 
    }, 
    "id": "HJK321", 
    "reportDate": "20150719" 
} 
+0

看看這一個想法...... HTTP:使用 //stackoverflow.com/a/31489211/145682 – deostroll

回答

5

您可以在對象屬性這樣的循環:

<div ng-repeat="(key,value) in model.attributes"> 
    <span>Name : {{key}}</span> 
    <span>Full Name : {{value.fullname}}</span> 
    <span>Age : {{value.age}}</span> 
</div> 

plunk

+0

如果什麼而不是,並作爲ng模型的關鍵? – devwannabe

+0

我會發表另一個問題。我注意到我的JSON假設使用數組作爲反對對象。 – devwannabe

1

你可以這樣說:

<div ng-app> 
    <div ng-controller="ClickToEditCtrl"> 
    <p>Your Data:</p> 
    <ul> 
     <li ng-repeat="(key, value) in data.attributes"> 
      firstname: {{key}}, fullname: {{value.fullname}}, age: {{value.age}} 
     </li> 
    </ul> 
</div> 

看看這個jsFi ddle:http://jsfiddle.net/vyzsw565/

1
<div ng-repeat='(user, description) in data.attributes'> 
     {{user}} 
     <div ng-repeat='(key, val) in description'> 
     <span>{{key}} : {{val}}</span> 
     </div> 
     <hr> 
    </div> 
0

看看這個工作Jsfiddle

<div ng-app="mainApp"> 
    <div ng-controller="mainController"> 
     <div ng-repeat="d in data"> 

      Name:  {{d.attributes.scott.fullname}} ,   
      Age :  {{d.attributes.scott.age}}  

      Name:  {{d.attributes.bill.fullname}} ,   
      Age :  {{d.attributes.bill.age}}  

     </div> 
    </div> 
</div> 
+0

它會工作,但每次數據發生變化時,它都會變成重構的噩夢 – devwannabe