angularionic-frameworkangular-lifecycle-hooks

Angular component - actualize data every time view is shown


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);
      });

Solution

  • 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.
    }