Update (High Level Idea/Concept)
To summarize I would like values to be dynamically updated in a controller which is binded to a Web Socket Service, which value change. Below is my attempt of how to solve the problem and the barrier which I am facing.
I would like to update the view model in the controller, each time that the socket pushes information in the service. To mimic the pushing of messages I am just calling a timeout every second, which in turn increase the value of health within the service, which I verified it works in console.
Though the value of the health within controller is never updated, and I am at a loss of of what I am doing wrong. Below are the snippets of relatable code.
This is the controller
(function () {
'use strict';
angular
.module('xxx')
.controller('DashboardController', ['$scope','$timeout', "$uibModal", "dashboardService", "syncService", DashboardController]);
function DashboardController($scope, $timeout, $uibModal, dashboardService, syncService) {
var vm = this;
// VERSIONS
vm.versions = {};
// HEALTH
vm.health= syncService.health;
// JUMP-START
activate();
//////////////
function activate(){
getVersions();
}
function getVersions(){
dashboardService.getVersions()
.then(function(data) {
vm.versions = data;
return vm.versions;
});
}
}
})();
This is the service
(function () {
'use strict';
angular
.module('xxx')
.service('syncService',['$timeout', syncService]);
function syncService($timeout) {
//Init WebSocket
var self = this;
self.health= 0;
updatedStatus();
///////////
....
function updatedStatus(){
$timeout(updatedStatus, 1000);
self.health += 1;
$timeout(angular.noop);
};
}
})();
Update
I would like to know how to make it work without using $scope. Example like in the accepted answer in the following question
thank you