Search code examples
angularjsauthenticationscopeionic-frameworkrootscope

Clear rootscope ionic framework


Hi I'm doing a login for ionic app.....and I'm using rootscope like a global variable to use in all controller (LoginCtrl,SalirCtrl) When the user is log-in I save his info in a rootscope variable and show that info in SalirCtrl.

BUt when user log-out and other user log-in his info is not present in SalirCtrl.

Someone knows about that.

LoginCtrl

if($scope.datos=='true') {//if token is true. User is log-in
   $rootScope.pNombre=data.persona.primerNombre;
   $rootScope.sNombre=data.persona.segundoNombre;
   $rootScope.pApellido=data.persona.primerApellido;
   $rootScope.sApellido=data.persona.segundoApellido;  
   $state.go('tabs.perfil');
}

SalirCtrl

.controller('SalirCtrl', function($scope, $state, $ionicPopup, ServUsuario,$rootScope,$ionicHistory) {
//para bloquear el boton atras
  $ionicHistory.nextViewOptions({
      disableAnimate: true,
      disableBack: true
  });
  //FIN para bloquear el boton atras
  $scope.pNombre = $rootScope.pNombre;//save in a scope variable rootscope
  $scope.sNombre = $rootScope.sNombre;
  $scope.pApellido = $rootScope.pApellido;
  $scope.sApellido = $rootScope.sApellido;
  //METODO SALIR
  $scope.salir = function() {
    var confirmPopup = $ionicPopup.confirm({
      title: 'Log-out',
      template: '¿Log-out?'
    });  
    confirmPopup.then(function(res) {
      if(res) {
        console.log('You are sure');
        $state.go('login');
        $scope.pNombre=" "; //When log-out is true. Variables equals empty
        $scope.sNombre=" ";
        $scope.pApellido=" ";
        $scope.sApellido=" ";
      } else {
        console.log('You are not sure');
      }
    });
  };  
  //FIN METODO SALIR
})

Finally I print that variables in perfil.html

{{pNombre}} {{sNombre}} {{pApellido}} {{sApellido}}

Thanks....!


Solution

  • My guess is that you're suffering from caching in the AngularJS UI-Router. In your route config, what happens when you do something like this?:

    $stateProvider.state('myState', {
       cache: false,
       url : '/myUrl',
       templateUrl : 'my-template.html'
    })
    

    By default Ionic caches the views as described here:

    http://ionicframework.com/docs/api/directive/ionNavView/

    When the views are cached your controllers don't reload during navigation. Let me know if that did the trick?

    Best,