I am attempting to connect to $rootscope in order to establish presence on a different server when the user logs in. Below is the code for my module.
angular.module('presence',
[
]
)
.run(function ($rootScope) {
$rootScope.$watch($rootScope.currentUser, function () {
console.log('change currentUser')
console.log($rootScope.currentUser)
presence.setGlobal({
u: $rootScope.currentUser,
s: 'on'
})
})
})
There is no controller involved because this is solely related to the global presence of the user and does not interact with the DOM.
Unfortunately, there seems to be an issue with the code as the watch only triggers once and does not respond on subsequent changes. Your assistance will be greatly appreciated. Thank you.
Edit: The login code is as follows:
$scope.login = function() {
$http.post('http://localhost:3000/login', $scope.user).success(function(res, status) {
if (res && res._id) {
$rootScope.currentUser = res
}
}).error(function(res, status) {
$scope.error = res.err
});
};
This code successfully updates in the DOM. For instance, it displays the username in the HTML like this:
a.user(ui-if="currentUser", ng-href="/user/{{currentUser._id}}") {{currentUser.username}}