我正在创建一个IONIC应用程序,使用API我能够将数据提供给我的组件。获得结果的代码是:
getIndianCollegeSearchData() {
this.showLoader();
this.apiService.getCollegeData().retry(3)
.subscribe(colleges => {
console.log(colleges);
this.loading.dismiss();
this.searchResults = colleges;
console.log(this.searchResults);
}, (err) => {
this.loading.dismiss();
}
);
我将HTML更改为,
<ion-list-header color="primary" *ngFor="let details of searchResults.result.engineering">
<ng-container *ngFor="let detail of details.colleges">
{{detail.title}}
</ng-container>
</ion-list-header>
我的JSON格式是:
{ "result": { "engineering": [ { "name": "Tamil Nadu", "colleges": [ { "id": "1", "title": "wdwd" }, { "id": "2", "title": "titlealsadasbum2" } ] }, { "name": "Kerala", "colleges": [ { "id": "3", "title": "titleqqwalbum2" }, { "id": "4", "title": "titleaasalbum2" } ] } ], "medicine": [ { "name": "Tamil Nadu", "colleges": [ { "id": "1", "title": "med-wdwd" }, { "id": "2", "title": "med-titlealsadasbum2" } ] }, { "name": "Kerala", "colleges": [ { "id": "3", "title": "med-titleqqwalbum2" }, { "id": "4", "title": "med-titleaasalbum2" } ] } ] } }
我在控制台中得到了输出:
但是在运行我的html文件时出现错误:
错误错误:未捕获(在承诺中):TypeError:无法读取属性 &#39;导致&#39;未定义的TypeError:无法读取属性&#39;结果&#39;的 在Object.eval上未定义[作为updateDirectives] (IndianstudiesPage.html:24)
我提到Observable type error: cannot read property of undefined 和 Cannot read property of 'xxx' of undefined。 两种解决方案都没有给我任何错误,但是html中没有显示任何值。
我添加了:<div *ngIf="searchResults">object data using NgIf: {{searchResults.result.engineering.name}}</div>
div中没有打印。
我是嵌套的json,需要以列表的形式循环遍历数组。
我在这做错了什么?