无法访问JSON属性值

时间:2015-11-12 10:29:41

标签: javascript json angularjs angularjs-scope

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

我有一个返回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的属性,例如:firstname using:

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

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

$scope.testJson Array您需要按索引获取元素,然后按Object获取属性

 {{testJson[0].firstName}}

Example

答案 1 :(得分:2)

你的testJson似乎是一个数组。所以{{testJson[0].firstName}}应该做到这一点!