Load a message depending on login or logout

I’m trying to load a message which depends on whether the user is signing in or out the session, but there must be something wrong because it only shows me the “Loading Information” (both signing in and out). I use the backView() method that obtains the previous view to the current one.

This is my code:

###login.js

angular.module(`moduleLogin`)
   .controller(`login_C`, login_C);

function login_C($scope){

$scope.click = function(){

$window.localStorage.logged = true;

$state.go(loading);

  }

}

###loading.js

angular.module(`moduleLoading`)
   .controller(`loading_C`, loading_C);

function loading_C($scope){

  if (!$window.localStorage.logged) {
      $scope.mensaje = "Loading Information";
  } else {
      $scope.mensaje = "Closing Session";
  }

}

###menu.js

delete $window.localStorage.logged;

If I sign in again (closing session and starting it again) then I get the right message, but not the first time that I log in.This is a small image explaining what I’m trying to do:

I am also facing like what you have now, but it is for clearing the cache and history… it doesn’t works in the first time I logout but the second and the rest try It succeeded… I don’t know what could be the error…

But I separate the problem, and what I did was create something basic with the three templates and the three javascript files , and feel the same, even thought it was some localStorage , and decided to use SQLite, and is the same.

What comes to the conclusion seeing this picture , it is that when a module running in the factory or service , it is with some instance begins to be restarted.

In fact, the $window.localStorage.logged value is checked just once (in loading.js) and it’s when the page is loaded for the first time . so the if isn’t executed after that, even if you open the loading page several times.
So to solve it, use the '$ionicView.enter' event. it’s triggered when you enter the page (loaded from cache or created for the first time).
to do so, put this in the loading.js file:

$scope.$on('$ionicView.enter', function () {
  if (!$window.localStorage.logged) {
      $scope.mensaje = "Loading Information";
  } else {
      $scope.mensaje = "Closing Session";
  }
});
1 Like

sorry, thanks for all, your answer was usefull, in my case, only change this $ionicView.enter for these $ionicView.beforeEnter

1 Like