Home Angular 8 How to make array push or empty in Angular 9 when getting data from API?

How to make array push or empty in Angular 9 when getting data from API?

by therichpost
0 comments
Angular 9, Angular 10

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

You may also like

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.