I am facing an issue with two controllers and one factory in my AngularJS application. The first controller sends an http request to a server, receives a string in response, and stores it in the factory. However, the second controller does not update with this new string as expected.
Here is the relevant code snippet:
...
.factory('user', function() {
return {
name: {str: ''},
passwordHashed: {str: ''},
userType: {str: 'none'},
};
})
.controller('nav', ['$scope', 'user', function($scope, user) {
$scope.allSites = [
//example of a page structure
{name: 'home', title: 'Home', allowed: ['none', 'admin']},
];
$scope.allowedSites = []; //displayed in navigation
angular.forEach($scope.allSites, function(site) {
if(contains(site.allowed, user.userType.str) != -1) $scope.allowedSites.push(site);
});
}])
.controller('loginCTRL', ['$scope', '$http', 'user', function($scope, $http, user) {
$http.get('url').success(function(data) {
//some code
user.name.str = $scope.enteredUsername;
user.passwordHashed.str = $scope.passwordHashed;
user.userType.str = data; //admin
});
});
The intended functionality is to update the navigation whenever the userType changes. However, after a login, the navigation does not refresh. I have tried different approaches, including structuring strings into separate objects, but the issue persists. Any insights or suggestions would be greatly appreciated.