AngularJS - How can I reference the property name within an ng-Repeat

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-26 07:58:18

问题


In addition to rendering the value of the properties in an object, I\'d also like to render the property name as a label. Is there a way to do this with ng-repeat? For example:

<ul>
    <li ng-repeat=\"option in data\">{{propertyName}}: {{option}}</li>
</ul>

Which might spit out something like this:

<ul>
    <li>Name: John</li>
    <li>Phone: (123) 456-7890</li>
    <li>Country: England</li>
</ul>

回答1:


Try this:

<ul>
    <li ng-repeat="(key,val) in data">{{key}}: {{val}}</li>
</ul>



回答2:


The problem with documentation is that it says (key, value) with that space ... it took me some time to figure out that because of that it doesn't work



来源:https://stackoverflow.com/questions/10954286/angularjs-how-can-i-reference-the-property-name-within-an-ng-repeat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!