简体   繁体   中英

angularjs ng-repeat using index rather than names

I use ng-repat to populate an html table with an array.

I have problems normalizing the naming of each property in the array.

Some users are very stubborn and now I have to find a quick work around, if possible.

my array AgendaItems sometimes comes like:

{"Agenda Item":"1","Legistar ID":"62236","Title":"Approval of Minutes","Meeting ID":"49837"}

Other times comes like:

{"AgendaItem":"1","LegistarID":"62236","Title":"Approval of Minutes","MeetingID":"49837"}

The html:

                    <tbody ng-repeat="ai in AgendaItems">   
                        <tr>
                            <td>{{ai.MeetingID}}</td>
                            <td>{{ai.AgendaItem}}</td>
                            <td>{{ai.LegistarID}}</td>
                            <td>{{ai.Title}}</td>
                        </tr>
                    </tbody>

Is there a way that I can use an index value instead that, for example: ai.[i] That way I do not have to worry whether the columns are name with or without a space in between?

Any help is much appreciated.

Thank you, Erasmo

You have implicit access in ng-repeat to scope properties like $index which the directive provides automatically.

However, using $index when iterating over an object rather than an array is rather questionable.

What you seem to be after is rather a way to iterate over an object's keys and values, which is not questionable and is in fact also supported by ng-repeat via the "(key, value) in object" syntax. The names key and value are arbitrary.

Here's how that would be written:

Firstly, for a single agendaItem , ai

<div>
  <div ng-repeat="(key, value) in ai">{{key}}: {{value}}</div>
</div>

Secondly, in the question's context

<tbody ng-repeat="ai in AgendaItems">
  <tr>
    <td ng-repeat="(key, value) in ai">{{value}}</td>
  </tr>
</tody>

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