【问题标题】:Unable to access JSON property values无法访问 JSON 属性值
【发布时间】:2015-11-12 10:29:41
【问题描述】:

这让我发疯了,我知道这一定是我想念的一些非常简单的事情!

我有一个返回 JSON 对象的服务:

[{"accountId":"0000004000006195","title":null,"firstName":"JOE","middleName":"BLOG","lastName":"BLOGGS","suffix":null,"primaryAddress":{"addressLine1":"TEST ADDRESS CLOSE","addressLine2":null,"addressLine3":"MY TOWN","town":"Washington","county":null,"country":"US","postcode":"PO9868"},"customerTelephones":[{"phoneNumber":"09876543455","type":"Alternate 1"},{"phoneNumber":"98654345676","type":"Alternate 2"}],"birthDate":108628400000}]

我正在尝试显示 JSON 的属性,例如名字使用:

app.controller('MainCtrl', function($scope) {
  $scope.name = 'World';

  $scope.testJson = [{"accountId":"0000004000006195","title":null,"firstName":"JOE","middleName":"BLOG","lastName":"BLOGGS","suffix":null,"primaryAddress":{"addressLine1":"TEST ADDRESS CLOSE","addressLine2":null,"addressLine3":"MY TOWN","town":"Washington","county":null,"country":"US","postcode":"PO9868"},"customerTelephones":[{"phoneNumber":"09876543455","type":"Alternate 1"},{"phoneNumber":"98654345676","type":"Alternate 2"}],"birthDate":108628400000}]
});

HTML:

<p>{{testJson}}</p>

<p>{{testJson.firstName}}</p>

但由于某种原因,{{testJson.firstName}} 显示为空白!

这是一个笨蛋:http://plnkr.co/edit/VWJfcXTPaDut2jqB4W3o?p=preview

有什么想法吗?

【问题讨论】:

    标签: javascript json angularjs angularjs-scope


    【解决方案1】:

    $scope.testJsonArray 你需要通过索引获取元素,然后通过Object 获取属性

     {{testJson[0].firstName}}
    

    Example

    【讨论】:

      【解决方案2】:

      您的 testJson 似乎是一个数组。所以{{testJson[0].firstName}} 应该可以解决问题!

      【讨论】:

        猜你喜欢
        • 2018-04-24
        • 1970-01-01
        • 1970-01-01
        • 2016-07-12
        • 2023-03-20
        • 2011-11-05
        • 2014-09-19
        • 2021-10-02
        • 1970-01-01
        相关资源
        最近更新 更多