Hello to all, welcome again on therichpost.com. In this post, I will tell you, How to make array push or empty in Angular 9 when getting data from API?
Post Working:
In this post, I am showing with video example that, how I am pushing the data to an array and also make that array empty during getting updated data.
Here is working code and please use or check carefully:
1. Here is my app.component.ts file and below code will push api data to an array:
... data = []; //Declare array variable constructor(private http: HttpClient) { this.http.get('http://localhost/mypage.php').subscribe(data => { this.data.push(data); // Pushing data to declared array variable console.log(this.data); }, error => console.error(error)); } ...
2. Here is my app.component.ts file and below code will make array variable empty:
refresh() { this.data.length=0; //Make array variable empty this.http.get('http://localhost/mypage.php').subscribe(data => { this.data.push(data); //Push updated data console.log(this.data); }, error => console.error(error)); }
if you have any query then please do comment below.
Jassa
Thank you
Recent Comments