I have the following service (pseudo code)
app.service('user', function($http) {
function login(email, password, callback) {
// login user
// set isLoggedIn to true
// assign the returned user object to userData
// issue callback
};
return {
isLoggedIn: false,
userData: null,
login: login
};
});
Here is my login controller:
app.controller('LoginController', function($scope, $http, $location, user) {
user.login($scope.email, $scope.password, function(isSuccess, data, status) {
if (isSuccess) { $scope.onSuccessfulLogin(); }
else { $scope.loginStatusMessage = data; }
});
$scope.onSuccessfulLogin = function() {
$location.path('/someOtherPage');
};
});
And here is the controller used in /someOtherPage
app.controller('SomeOtherPageController', function($scope, $http, $modal, user) {
// lots of stuff in here
// I put a breakpoint on the first line and user.isLoggedIn is false
// even though it was definitely set to true when the user logged in.
)};
Once the callback has been issued when login in using the user
service, if the login was successful the user is taken to a different page with a different controller, where the above user
service is injected. The problem is that if the login is successful, although I can see the isLoggedIn
and userData
variables being assigned correctly, in the new controller they remain false
and null
respectively.
Have I missed something here ? I need the values to be the same whenever user
is injected like a singleton.
Thanks