I have an AngularJS factory
named 'userFactory' that I'm using:
app.factory('userFactory', function($window) {
var auth = {
isLogged: false,
user: "a",
check: function() {
if ($window.sessionStorage.token) {
this.isLogged = true;
} else {
this.isLogged = false;
delete this.user;
}
}
}
return auth;
});
To test it out, I added some static content inside a controller
. On clicking a button
, the following code runs:
// replace "a" with the following
app.controller('LoginCtrl', ['$scope', '$window', '$location', 'userFactory',
function ($scope, $window, $location, userFactory) {
$scope.login = function(){
var userCredentials = {
"username": $scope.username,
"password": $scope.password
};
if (userCredentials.username == "amir" && userCredentials.password == "blum") {
userFactory.isLogged = true;
userFactory.user = {
"name": "amir",
"surname": "blumenfeld"
};
$location.path("/customer");
} else {
alert("NO");
}
}
}
]);
Then, trying to display the surname "blumenfeld" on the page:
function menuCtrl($scope, $location, userFactory, $window) {
$scope.user = userFactory.user.surname;
}
The expected output should be "blumenfeld" here:
<ul ng-controller="menuCtrl">
<li><a href="#home">{{ user }}</a></li>
</ul>
But unfortunately, all I see is just an empty space.
I need assistance in identifying where I went wrong and how I can correct it.
Thank you!