sobha
sobha

Reputation: 175

How to call this angular function on load

I am having a async function getDataStandard() which needs to be executed on click.I need to get the function automatically done without clicking. How to do that. Please help me as I am new to ionic.

async getDataStandard() {
    let loading = await this.loadingCtrl.create();
    await loading.present();

    this.http.get('https://www.labourguide.co.za/LabourguideApi/index.php?q=mostRecentPublications').pipe(
      finalize(() => loading.dismiss())
    )
    .subscribe(data => {
console.log("Testing ...............1");
      this.data = data;
console.log(data);
console.log("Testing ..............2");
    }, err => {
      console.log('JS Call error: ', err);
    });
  }

This is the ionic part

 <ion-button expand="block" (click)="getDataStandard()">getDataStandard</ion-button>

Upvotes: 1

Views: 8422

Answers (4)

Muhammed Albarmavi
Muhammed Albarmavi

Reputation: 24454

angular has several lifecycle hooks they are just an method of the component that manage and run by angualr,there are three method that sutable for your need ngOninit ,ngAfterContentInit and ngAfterViewInit .

ngOnInit() {
 this.getDataStandard();  
} 

ngAfterContentInit() {}

ngAfterViewInit() {}

all previes method will run once in sequence so if you want the function will run as soon is possibe use ngOninit,or it you want to run after the component fully initialized I will choses ngAfterViewInit.

Lifecycle Hooks 🌐

Upvotes: 1

Vicky Kumar
Vicky Kumar

Reputation: 44

You can call this function from ngOnInit(){}.

Upvotes: 3

zmag
zmag

Reputation: 8251

Just call getDataStandard() in ngOnInit().

...
ngOnInit() {
  this.getDataStandard();
}
...

Upvotes: 2

Call it on your ngOnInit

export class App implements OnInit{
  constructor(){
     //called first time before the ngOnInit()
  }

  ngOnInit(){

    this.getDataStandard() ;

  }
}

Upvotes: 10

Related Questions