I am creating a IONIC Application, Using API I am able to get data to my component. Code to get result is:
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();
}
);
I changed my HTML as,
<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>
my JSON Format is:
{ "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" } ] } ] } }
I got the output in console as:
But when running my html file i got an error:
ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'result' of undefined TypeError: Cannot read property 'result' of undefined at Object.eval [as updateDirectives] (IndianstudiesPage.html:24)
i referred to Observable type error: cannot read property of undefined and Cannot read property of 'xxx' of undefined. Both solutions gives me no errors but no value is displayed in html.
I added : <div *ngIf="searchResults">object data using NgIf: {{searchResults.result.engineering.name}}</div>
Nothing prints in div.
Mine is nested json and need to loop through the array as a list.
what am i doing here wrong?