I'm using ionic. I have a really basic question on actualizing data in component.
I fetch data in components ngOnInit
method over httpservice.
This is only done once but it should be done every time this component is shown in view.
What is the appropriated way to do this.
ngOnInit()
myService.getData().subscribe(response => {
}, error => {
console.log(error);
});
CodePudding user response:
Just call a method in subscribe
.
ngOnInit() {
myService.getData().subscribe(response => {
this.someMethod(response); // Add this line.
}, error => {
console.log(error);
});
}
someMethod = (response) => {
// Write your logic here that you have to do after you fetched your data.
}
CodePudding user response:
In Ionic ngOnInit
won't call when the page is revisited. The reason is Ionic doesn't remove the components from the DOM once they are visited.
So Ionic does follow different sets of lifecycle hooks. You have to call ionViewDidEnter
hook to call something on component init.
ionViewDidEnter() {
myService.getData().subscribe(response => {
}, error => {
console.log(error);
});
}