View is loaded before function call to Factory is returned with value

Hello, I’m new to ionic and I’m having problem with ionic-view not being updated due to a service function call. If I write the content of the function in the controller then the view is updated fine.

In the controller, if I use this, my view is not updated with the userName. “Chats” is the factory name.

  Chats.getUserName().then(function (result) {
    $scope.userName = result;
  });

If I write the content of getUserName() in the controller, then my view is updated successfully with the userName:

  $cordovaFile.readAsText(cordova.file.dataDirectory, $scope.localUserFile)
  .then(function (result) {
    $scope.user = JSON.parse(result);
    $scope.userName = $scope.user.name;
 });

My view:


  
    
Hello, !

I really want to use Service / Factory but I just can’t get it working this time. It seems that the Factory is “slow” in returning the value, hence the view is updated before $scope.userName is updated with the returned value from Factory.

Please help and point me in the right direction.

Thank you