Reputation: 175
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
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.
Upvotes: 1
Reputation: 8251
Just call getDataStandard()
in ngOnInit()
.
...
ngOnInit() {
this.getDataStandard();
}
...
Upvotes: 2
Reputation: 5202
Call it on your ngOnInit
export class App implements OnInit{
constructor(){
//called first time before the ngOnInit()
}
ngOnInit(){
this.getDataStandard() ;
}
}
Upvotes: 10