如何从 AngularJS 模板中的嵌套 JSON 数组中获取值? [英] How to get value from a nested JSON array in AngularJS template?

查看:43
本文介绍了如何从 AngularJS 模板中的嵌套 JSON 数组中获取值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面有一些 JSON 数据:

<代码>{曲目":[{专辑":{"发布": "2013","href": "spotify:album:3qGeRY1wt4rrLIt1YuSwHR","name": "The Marshall Mathers LP2 (Deluxe)",可用性": {领土":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 PEPH PL PT PY RO SE SG SI SK SV TR TW UY"}},"name": "怪物","人气": "0.94",外部 ID":[{"类型": "isrc","id": "USUM71314082" <-- 我想要这个值}],长度":250.188,"href": "spotify:track:5U8hKxSaDXB8cVeLFQjvwx",艺术家":[{"href": "spotify:artist:7dGJo4pcD2V6oG8kP0tJRR",姓名":阿姆"},{"href": "spotify:artist:5pKCCKE2ajJHZ9KAiaK11H",名称":蕾哈娜"}],曲目编号":12"},...]}

我可以删除其他值,例如专辑名称、发行日期等,但不能删除外部 ID.这是我在我的 angular 模板中尝试过的两种方法:

<p>曲目名称:{{ track.name }}</p><p>专辑发行日期:{{ 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>-->

这是我想出的两种解决方案,但似乎都不起作用.第一个(未注释掉)返回 0,第二个根本不显示spotify id:"行.

有人知道怎么做吗?这看起来很简单,但我似乎无法理解它.谢谢.

解决方案

要访问包含破折号的属性,您需要使用括号表示法:

<p>Spotify id: {{track['external-ids'][0].id}} </p>

否则解析器将解释为减法运算符:

track.external - ids[0].id

演示: http://plnkr.co/edit/gIyXg8y0Pn1TuK0uI6qD?p=预览

I have some JSON data below:

{
"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"
    },
    ...
]}

I am able to take out other values like album name, release date etc, but not the external-ids id. these are the two ways i have tried in my angular template:

<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>

those are the two solutions i came up with but neither seem to work. The first one (not commented out) returns 0, the second one doesn't display the "spotify id:" line at all.

Does anybody know how to do this? it seems pretty straightforward but i just cant seem to get my head around it. Thanks.

解决方案

To access a property that contains a dash you need to use the bracket notation:

<p>Spotify id: {{track['external-ids'][0].id}} </p>

Otherwise the parser will interpret is as the subtraction operator:

track.external - ids[0].id

Demo: http://plnkr.co/edit/gIyXg8y0Pn1TuK0uI6qD?p=preview

这篇关于如何从 AngularJS 模板中的嵌套 JSON 数组中获取值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆