javascript-如何从AngularJS模板中的嵌套JSON数组中获取价值?
作者:互联网
我下面有一些JSON数据:
{
"tracks": [
{
"album": {
"released": "2013",
"href": "spotify:album:3qGeRY1wt4rrLIt1YuSwHR",
"name": "The Marshall Mathers LP2 (Deluxe)",
"availability": {
"territories": "AD AR AT AU BE BG BO BR CH CL CO CR CY CZ DE DK DO EC EE ES FI FR GB GR GT HK HN HR HU IE IS IT LI LT LU LV MC MT MY NI NL NO NZ PA PE PH PL PT PY RO SE SG SI SK SV TR TW UY"
}
},
"name": "The Monster",
"popularity": "0.94",
"external-ids": [
{
"type": "isrc",
"id": "USUM71314082" <-- I want this value
}
],
"length": 250.188,
"href": "spotify:track:5U8hKxSaDXB8cVeLFQjvwx",
"artists": [
{
"href": "spotify:artist:7dGJo4pcD2V6oG8kP0tJRR",
"name": "Eminem"
},
{
"href": "spotify:artist:5pKCCKE2ajJHZ9KAiaK11H",
"name": "Rihanna"
}
],
"track-number": "12"
},
...
]}
我可以取出其他值,例如专辑名称,发行日期等,但不能提取外部ID.这是我在角度模板中尝试的两种方法:
<div class="panel panel-success" ng-repeat="track in something.tracks">
<p>Track name: {{ track.name }}</p>
<p>Album release date: {{ track.album.released }}</p>
<p>Spotify id: {{track.external-ids[0].id}} </p>
<!-- <span ng-repeat="obj in track.external-ids">
<p>Spotify id: {{ obj.id }}</p>
</span> -->
</div>
这些是我想出的两个解决方案,但似乎都不起作用.第一个(未注释掉)返回0,第二个完全不显示“ spotify id:”行.
有人知道怎么做这个吗?似乎很简单,但我似乎无法解决.谢谢.
解决方法:
要访问包含破折号的属性,您需要使用方括号符号:
<p>Spotify id: {{track['external-ids'][0].id}} </p>
否则,解析器将解释为减法运算符:
track.external - ids[0].id
演示:http://plnkr.co/edit/gIyXg8y0Pn1TuK0uI6qD?p=preview
标签:angularjs,ng-repeat,json,javascript 来源: https://codeday.me/bug/20191121/2055725.html