Categories

Thursday, November 21, 2024
#919814419350 therichposts@gmail.com
Angular 8Angular 9Angular7

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

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

therichpost
the authortherichpost
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know WordPress, Core php, Angularjs, Angular 19, MedusaJs, Next.js, Bootstrap 5, Nodejs, Laravel, Codeigniter, Shopify, Squarespace, jQuery, Google Map Api, Vuejs, Reactjs, Big commerce etc.

Leave a Reply

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