[英]Access HTTP nested JSON data in Angular
Here's the component in question: 这是有问题的组件:
@Component({
selector: 'my-app',
template: `
{{tests | json}}
<div *ngFor='let test of tests'>
<div *ngFor='let x of test.A.B'>
<div *ngFor='let y of x.C.D'>
writerid:{{y.writerid}} <br>
content:{{y.content}} <br>
writedt:{{y.writedt }} <br>
</div>
</div>
</div>
`,
})
public tests: any[];
The constructor gets the JSON data from an API here: 构造函数从此处的API获取JSON数据:
constructor(private _http: Http) {
_http.get(this.API_URI).subscribe(result => {
this.tests = result.json();
});
}
This is my JSON data from http(API_URI)
, printed by {{tests | json}}
这是我来自http(API_URI)
JSON数据,由{{tests | json}}
{{tests | json}}
(in @component, template): {{tests | json}}
(在@component,模板中):
{
'A': {
'B': [{
'C': {
'D': [{
'content': 'content_test1',
'writedt': '2017-02-08 00:00:00',
'writerid': 'writerid_test1'
}, {
'content': 'content_test2',
'writedt': '2017-02-08 00:00:00',
'writerid': 'writerid_test1'
}, {
'content': 'content_test3',
'writedt': '2017-02-08 00:00:00',
'writerid': 'writerid_test2'
}, {
'content': 'content_test4',
'writedt': '2017-02-08 00:00:00',
'writerid': 'writerid_test2'
}]
}
}]
}
}
The error I get is this: 我得到的错误是这样的:
Cannot find a differ supporting object '[object Object]' of type 'object'. 找不到类型为“对象”的其他支持对象“ [对象对象]”。 NgFor only supports binding to Iterables such as Arrays. NgFor仅支持绑定到Iterables,例如数组。
How can I fix it? 我该如何解决?
Look into your json, it has object but ngFor works for array types 查看您的json,它具有对象,但是ngFor适用于数组类型
<div *ngFor='let x of tests.A.B'>
<div *ngFor='let y of x.C.D'>
writerid:{{y.writerid}} <br>
content:{{y.content}} <br>
writedt:{{y.writedt }} <br>
</div>
</div>
</div>
That is the reason you get this error. 这就是您收到此错误的原因。
Mistakes 误区
<div *ngFor='let test of tests'>
- tests is of type object and not a collection of objects or array. <div *ngFor='let test of tests'>
-测试属于对象类型,而不是对象或数组的集合。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.