1
I created a Service to share a variable with my entire application, but the value is not being updated:
- The service makes only one query on the server to return the number of pending users
- And I would like this value to be shared in several application locations, in this case I’m showing how it would be with the header, which 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
};
}
})();
Exactly that, the $Scope. $apply worked, now I’m seeing how I can do it using the "controllerAs", thank you very much
– Daniel Kuroski