In angular 6 while I am binding a nested model to view, its giving error as "Can't read property of undefined".
Model-
export class Country {
CountryID: number;
CountryCode: string;
CountryName: string;
CurrencyCode: string;
}
export class Data {
Country: Country[]
}
export class CountryMaster {
IsSuccess: boolean;
ErrorCode: string;
ErrorDescription: string;
Data: Data;
}
Component
import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { CountryMaster, Country,Data } from '../Shared/Model/CountryModel';
@Component({
selector: 'app-shopping',
templateUrl: './shopping.component.html',
styleUrls: ['./shopping.component.css']
})
export class ShoppingComponent implements OnInit {
constructor(public httpCl: HttpClient) { }
ListOfCountry: Data;
countries: Country[];
ngOnInit() {
this.BindCountry();
}
BindCountry(): void {
this.httpCl.get("http://localhost:59049/api/Country/GetCountry").subscribe(
(successData: CountryMaster) => {
this.ListOfCountry = successData.Data;
},failData => {
//alert(failData);
});
}
}
and template
<div *ngFor="let ctry of ListOfCountry.Country">
<div>
<label>{{ctry.CountryCode}}</label>
<label>{{ctry.CountryName}}</label>
</div>
</div>
Though it bind the data but also give the error in console. When i pass the list of Country model to view then it works fine or when i added a condition *ngIf="ListOfCountry" then there is no error. Can anyone help me why its happening when I am binding with nested object
