简体   繁体   中英

How to use ng-repeat for nested dictionaries in AngularJs?

I have a dictionary where the value is itself an object. I want to show the entries in a table using ng-repeat .

Here is the dictionary:

{
        'adam': {
            c1: "a",
            c2: "b"
        },
        'amalie': {
            c1: "c",
            c2: "d"
        }
    };

I have visited this question but it talks about simple key-value pairs.

Here is a jsfiddle .

I want it to be shown as:

NAME c1 c2
adam a b
amalie c d
<ul>
    <li ng-repeat="(name, ages) in items">              
        {{name}} 
        <span ng-repeat="age in ages">
            {{age}}
        </span>        
    </li>
</ul>

jsfiddle

<div ng-app ng-controller="MyCtrl">
    <ul>
        <li ng-repeat="(name, age) in items">{{name}}: {{age.c1}} : {{age.c2}}</li>
    </ul>
</div>

use like this...

Check this updated jsFiddle. Hope this helps.

<div ng-app ng-controller="MyCtrl">
    <table>
        <tr>
            <td>Name</td>
            <td>C1</td>
            <td>C2</td>
        </tr>

        <tr ng-repeat="(name, age) in items">
            <td>{{name}}</td>
            <td>{{age.c1}}</td>
            <td>{{age.c2}}</td>
        </tr>

    </table>
</div>

http://jsfiddle.net/5dh0d8nd/2/

It can be like this:

<div ng-app ng-controller="MyCtrl">
    <table style="width:50%">
         <tr>
            <th>NAME</th>
            <th>C1</th>
            <th>C2</th>
         </tr>   
         <tr ng-repeat="(name,age) in items">
            <td>{{name}} </td>
            <td> {{age.c1}}</td>        
            <td> {{age.c2}}</td>
         </tr>
   </table>
</div>
<table style="width:100%">

</table>

See this Fiddle .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM