Arrays 在ngFor的帮助下,在Angular5中迭代JSON数组
我正在使用Angular5构建一个项目。我陷入了Arrays 在ngFor的帮助下,在Angular5中迭代JSON数组,arrays,json,angular,typescript,angular5,Arrays,Json,Angular,Typescript,Angular5,我正在使用Angular5构建一个项目。我陷入了*ngFor。 我有一个模型类,如下所示: export class FetchApi { value: Array<String>; api_status: string; api_version: string; data: Array<String>; } API返回的JSON如下所示: { “api_状态”:“200”, “api_版本”:“1.0”, “数据”:{ “特色”:[{ “id”:1,
*ngFor
。
我有一个模型类,如下所示:
export class FetchApi {
value: Array<String>;
api_status: string;
api_version: string;
data: Array<String>;
}
API返回的JSON如下所示:
{
“api_状态”:“200”,
“api_版本”:“1.0”,
“数据”:{
“特色”:[{
“id”:1,
“视频”:“视频”,
“uid”:,
“名称”:“名称”,
“说明”:”
}]
}
}
我想在
的帮助下在我的HTML页面上呈现响应,但是,出现了一个错误:
FetchApiComponent.html:22错误:找不到类型为“object”的不同支持对象“[object object]”。NgFor只支持绑定到数组之类的可重用文件。
在NgForOf.push../node_modules/@angular/common/fesm5/common.js.NgForOf.ngOnChanges(common.js:3121)
在checkAndUpdateDirectiveInline(core.js:8941)
在checkAndUpdateNodeLine(core.js:10209)
在checkAndUpdateNode(core.js:10171)
在debugCheckAndUpdateNode(core.js:10804)
在debugCheckDirectivesFn(core.js:10764)
在Object.eval[作为updateDirectives](FetchApiComponent.html:22)
在Object.debugUpdateDirectives[作为updateDirectives](core.js:10756)
在checkAndUpdateView(core.js:10153)上
在callViewAction(core.js:10394)
请让我知道我做错了什么,因为我是安格拉尔的新手。如何在HTML页面上获取JSON数组 你应该写
您应该在html中这样做,以处理html中的错误
<tr *ngFor="let fetch-api of fetchApi?.data?.featured">
希望这对你有帮助 我这样做了,现在我得到了另一个错误:
error-TypeError:无法读取Object.eval[as-updateDirectives](FetchApiComponent.html:22)处Object.debugUpdateDirectives[as-updateDirectives](core.js:10756)处checkAndUpdateView(core.js:10153)处callViewAction(core.js:10394)处未定义的属性“featured”在checkAndUpdateView(core.js:10159)的execComponentViewsAction(core.js:10336)在callViewAction(core.js:10394)的execEmbeddedViewsAction(core.js:10357)在checkAndUpdateView(core.js:10154)在callViewAction(core.js:103)的execComponentViewsAction(core.js:10336)
在console.log(this.fetchApi)之后您在控制台中看到了什么在OnInit函数中?我得到了JSONI知道你得到了json,它的内容是什么,把它粘贴在这里Observable{{u isScalar:true,{u subscribe:ƒ,value:{…}}value:api_状态:“200”api_版本:“1.0”数据:特色:(10)[{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…}最新的:(10)[…},{…},{…},{…},{…}、{…}、{…}、{…}、{…}、{…}、{…}、{…}、{…}、{…}、{…}、{…}、{…}、{…}、{…}、{…}、{…}、{…}、{…}、{…}、{…}、{…}、{…}、{…}、{…}、{…}、{…}、{…}、{…}、{…}、{…}、{…}、{…}、{…}、{…}、{…}、{…}、}、{…}、{…}、{…}、{uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。
ngOnInit() {
const scope = this;
this.userService.getUsers()
.subscribe( data => {
this.fetchApi = of(data);
console.log(this.fetchApi)
//this.fetchApi = data;
});
};
<tr *ngFor="let fetch-api of fetchApi?.data?.featured">
this.userService.getUsers()
.subscribe((data) => {
this.fetchApi = data;
console.log(this.fetchApi)
//this.fetchApi = data;
});