I am making angular application and where i am having an empty array like,
users: any = [];
Then i am making a service call in ngOnInit
to store the data into users
array like,
ngOnInit() {
//Getting the data from json
this.httpClient.get('https://api.myjson.com/bins/n5p2m').subscribe((response: any) => {
console.log("response", response.data);
response.data.forEach(element => {
//Pusing the data to users array
this.users.push(element);
});
})
//Trying to get the complete array after push of the element from the service response data
console.log("users array", this.users);
//But it shows empty array
}
But i am unable to get the data in console.log("users array", this.users);
as because the service call makes a delay..
How to push the data into this.users
and when we call outside of the service it should have the filled data and not empty as like now..
Also made a working stackblitz regarding it https://stackblitz.com/edit/angular-q3yphw
Please see the console which has current result..
Current result in console.log("users array", this.users);
is empty array [].
So i am expecting the following result in console.log("users array", this.users);
outside the service and inside ngOnInit
,
[
{"id":1,"value":"User One"},
{"id":2,"value":"User Two"},
{"id":3,"value":"User Three"}
]
As i am new in angular kindly help me to achieve the expected result..