Ionic 4 - Tabs data refresh issue

Hello,

I have been practising Ionic 4 for a few days and I have a problem that I can’t solve on my own. Basically I have a tabs app (created with ionic start tabs). I also have a service and in my app and a function in it that returns a string. In my tab controller I declare a new variable and call the service function to assign its return value to the variable. However, this string does not automatically update in the view if it changes. I have tried reassigning the value to it inside ionViewWillEnter but this only gets called when the tab is loaded for the first time. Any ideas how I should address this?

export class Tab3Page {
  constructor (public storage: Storage, public router: Router, public platform: Platform, public ds: DataService) {}
  public loggedUser: any = this.ds.getLoggedUser();
  ionViewWillEnter() {
    this.refresh();
  }

  refresh() {
    let loggedUser = this.ds.getLoggedUser();
    console.log("Logged user: ", loggedUser);
    this.loggedUser = loggedUser;
  }

  logout () {
    //logout stuff
  }
}

In my view I use to display logged user’s username like:

{{ loggedUser.username }}

Thanks.

@Vauhtisilakka
Hi, Did you find any solution for this issue?
I also stuck at this level.
Any help would be appreciated.

@cdtilwani
Hello,

I found a solution. I created a new function inside the page controller like:

  getLoggedUser() {
    return this.ds.getLoggedUser();
  }

And then referenced this function inside the template:

{{ getLoggedUser() }}
1 Like