Scenario
- I have a
UserService service that supports a (boolean) user sessionStatus . - As a result, [LOGOUT] is conditionally displayed on
ng-show=sessionStatus (that is, if it is not logged in (false), no show). sessionStatus of ViewController should always match UserService ... right?- If you click [LOGOUT] when it is visible, it will make some errors in the log, the value of
sessionStatus will change, and the view should refresh with the new result of ng-show...
Problem
- Currently pressing the exit button is not like updating var UserService.sessionStatus?
- How to save the value of the variable $ scope.sessionStatus when UserService.logout () occurs and change UserService.sessionStatus?
- How to match changing $ scope with ng-show?
Files
View
<a ng-show="sessionStatus" ng-click="logout()">Logout</a>
ViewController
app.controller('AppController', function($scope, $interval, $http, UserService) { $scope.logout = function() { UserService.logout(); }
User service
NB: appUser is a nested global var in the head of HTML (hacker fix until I get session / cookie work)
app.factory('UserService', function($http) { var pre; var sessionStatus; function init() { // Logged in : Logged out pre = appUser.user != undefined ? appUser.user : { name: 'Logged out', uid: '0' }; sessionStatus = pre.uid != "0" ? true : false; } function resetSession() { appUser = null; init(); } init(); return { sessionStatus: function() { return sessionStatus; }, // update on change! logout: function() { $http.get("/logout").then(function (data, status, headers, config) { resetSession(); }) } }; });
source share