Reputation: 217
I noticed that the ngOnInit()
method gets not called when I come back to the page which is already instanced. I have to use any other method for this? I need a method which is called everytime when he is visiting the specific page.
EDIT
Tested already onPageWillEnter()
but it gets not fired in Ionic 2
Upvotes: 11
Views: 12661
Reputation: 2452
Angular components have life cycle methods, once a component a loaded, it will call ngOnInit() method and then in order to properly close it, we need to emit close event this.onClose.emit(), so when the component called 2nd time, it will trigger ngOnInit() again.
Example: close is a custom method where we call onClose event.
close(): void
{
// do some custom logic here...
this.onClose.emit();
}
But make sure your component class implements OnInit.
Upvotes: 0
Reputation: 29624
Check Lifecycle Events section in the link.
You can use ionic 2 lifecycle hook
ionViewWillEnter(){
//your methods
}
Upvotes: 15
Reputation: 5374
To recall ngOnInit everytime when page is visiting, you should use ngOnDestroy. For example, if your component content depends on code in url, you should use OnDestroy, in this way:
export class GRMTasksComponent implements OnInit, OnDestroy {
subParams: Subscription;
ngOnInit() {
this.subParams = this._route.params.subscribe(params => {
//some code...
});
}
ngOnDestroy() {
this.subParams.unsubscribe();
}
}
Upvotes: 4
Reputation: 657406
If you change a route so that only a parameter value changed, then the component is reused.
You can use
constructor(router:Router) {
router.params.subscribe(val => myInit());
}
to call your initialization code instead of using ngOnInit()
in such cases.
Upvotes: 5