I am trying to load a json data from backend service to my angular app. I want to load/push data into an array. Below is what my data from backend looks like
{ "Record":[
{
"Name":"John",
"Place":"Seattle"
},
{
"Name":"AS2",
"Place":"DC"
}]}
Here is my array model class in typescript
export class EventMaster {
public name: string;
public cityName: string;
constructor (name: string, cityName: string){
this.cityName = cityName;
this.name = name;
}
}
Here is my typescript code which I am using to fetch data from back-end and load into array
private eventMaster: EventMaster[] = [
new EventMaster('Loan', 'Approved' )
];
onFetchData(form: HTMLFormElement){
console.log(form);
console.log(form.value);
this.nameCity = new NameCity(form.value.name, form.value.city);
this.fetchDataService.fetchData(this.nameCity).subscribe(
(data: EventMaster[]) => {
console.log(data);
this.eventMaster.push(...data);
console.log(this.eventMaster.length);
}
);
}
Here is my http get call
fetchData(nameCity: NameCity) {
return this.http.get<EventMaster[]>(this.baseURL);
}
When I try to check the length of array it always shows as 1 and not 2. What am I doing wrong?
this.eventMaster = [...this.eventMaster, ...data];instead of push.