Im trying to build a login modal that will update templates (just basic user info - avatars,names,etc..) across different controllers. Loosely following this example, my approach has been to bind service variables directly within my partials like this:
partial:
<div ng-controller="TopBarControl">
<span>{{ userService.getInfo() }}</span>
</div>
service:
.service('userService', function($http) {
this.userInfo = {
isLogged: false
}
this.getInfo = function() {
return this.userInfo;
}
this.loginInit = function(userName, password) {
$http.get('http://example.com/?json=get_nonce&controller=auth&method=generate_auth_cookie').success(
function(data, status, headers, config) {
var nonce = data.nonce;
$http.get('http://example.com/?json=auth/generate_auth_cookie&nonce='+nonce+'&username='+userName+'&password='+password).success(function(data, status, headers, config) {
if (data.status == 'ok') {
this.userInfo = [
{isLogged: true},
{username: data.user.username},
{firstName: data.user.firstname},
{lastName: data.user.lastname},
{avatar: data.user.avatar}
];
return userInfo;
}
/* handle errors and stuff*/
});
}
})
controllers:
.controller('TopBarControl', function($scope, $modal, userService) {
$scope.userService = userService;
$scope.openLogin = function() {
var modalInstance = $modal.open({
templateUrl: 'views/modal.html',
controller: ModalInstanceCtrl,
size: 'lg'
});
}
});
var ModalInstanceCtrl = function ($scope, $modalInstance, userService) {
$scope.login = function () {
userService.loginInit(this.userName, this.password);
};
$scope.ok = function () {
$modalInstance.close($scope.returnResolveVariable);
};
};
So, the login works. Data is sent to the server and the correct data is successfully brought back to the app. The two-way binding sorta works - every time there is a change, the template calls back to .getInfo() and spits out the value of userInfo. The problem is that the value of userInfo never changes. I can't figure out if its something weird about how I am setting the variable in loginInit or if there is something that I fundamentally don't understand about how services handle variables like this.
userInfoand the service'suserInfovariables. They aren't the same variable.