I created a Service to be able to share a variable with my entire application, but the value is not being updated:
- The service only queries the server to return the number of pending users
- And I'd like this value to be shared across multiple application locations, in this case I'm showing you what it would look like with the header, it shows a pending user counter
- After the query, the returned value is correct and I give a console.log in the variable "service.pendingUsersCount" and the value is correct, but in the view it remains the same
// MEU SERVICE
(function() {
'use strict';
angular
.module('meuModulo')
.factory('UserPendingService', UserPendingService);
UserPendingService.$inject = [
'UserService',
'Restangular'
];
function UserPendingService(UserService,
Restangular) {
var service = {};
service.pendingUsersCount = 2;
_activate();
return service;
function _activate() {
UserService.searchPendingUsers()
.then(_successFn, _errorFn);
function _successFn(response) {
service.pendingUsersCount = response.data.count;
// O valor é imprimido corretamente, porém a variavel "pendingUsersCount" não é atualizada na view
console.log(service.pendingUsersCount);
}
function _errorFn(response) {
}
}
}
})();
// UM DOS LOCAIS DA APLICAÇAO A ONDE ESTE SERVICE É UTILIZADO
(function () {
'use strict';
angular
.module('meuModulo')
.directive('headerNotification', headerNotification)
.controller('HeaderNotificationController', HeaderNotificationController);
headerNotification.$inject = [
'APP_PATH'
];
function headerNotification(APP_PATH) {
return {
templateUrl: 'header-notification.view.html',
restrict: 'E',
replace: true,
controller: HeaderNotificationController,
controllerAs: 'headerNotificationCtrl',
bindToController: true,
}
}
HeaderNotificationController.$inject = [
'UserPendingService'
];
function HeaderNotificationController(UserPendingService) {
var vm = this;
vm.models = {
usersCount: UserPendingService.pendingUsersCount
};
}
})();