() . , , . , .
, Angular , . , $scope.loggedIn. , .
window.loggedIn, . , . , .
, :
,
factory, . , - . ,
(, $rootScope? ?)
:
.controller("ApplicationController", function($scope, AuthService) {
$scope.isAuthenticated = function() {
return AuthService.isAuthenticated();
};
});
.service("AuthService", function($http, UserSession) {
this.login = function() { ... };
this.logout = function() { ... };
this.isAuthenticated = function() {
return UserSession.exists();
};
});
Factory
.factory("UserSession", function() {
var currUserId = null;
return {
createSession: function(userId) {
currUserId = userId;
},
destroySession: function() {
currUserId = null;
},
exists: function() {
return currUserId !== null;
}
};
});
HTML
<div ng-if="isAuthenticated()"> ... </div>
, , . , , .