Json的代码工作正常,如果我调用页面与{{result.title}}
但如果我要打电话给author
孩子,JSON元素不起作用儿童JSON元素Angularjs
控制器
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope,$http) {
$http.get('post.json')
.then(function (response){
console.log(response.data);
$scope.results = response.data.pages,
$scope.resultauthor = response.data.author;
});
的Json示例
{
"pages": [
{
"id": 2,
"type": "page",
"modified": "2016-08-09 17:28:01",
"categories": [],
"author": {
"id": 1,
"name": "admin",
"description": ""
},
}
的Html
<div ng-repeat="item in resultauthor">
{{ item.name }}
</div>
我在哪里我的代码的问题呢?
您确定您的'$ http.get'将返回一个包含'author'对象?鉴于您提供的示例json,它看起来像'author'是'pages'数组中对象的子节点。 – Lex
Hi Lex,示例json它在注释之上它存储在文件“post.json”中 – Max
因此'$ scope.resultauthor'总是会被定义为undefined。虽然,我不明白'{{result.title}}'是如何工作的,因为你没有显示'$ scope.result'变量,你提供的示例json在任何地方都没有'title'属性。也许你应该编辑你的问题来提供[MCVE],然后可以提供更好的建议。 – Lex