javascript - Dynamically Update Model From Web Socket -


update (high level idea/concept)

to summarize values dynamically updated in controller binded web socket service, value change. below attempt of how solve problem , barrier facing.


i update view model in controller, each time socket pushes information in service. mimic pushing of messages calling timeout every second, in turn increase value of health within service, verified works in console.

though value of health within controller never updated, , @ loss of of doing wrong. below snippets of relatable code.

this 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 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 know how make work without using $scope. example in accepted answer in following question

thank you

afaik, you'll need put watch on service variable. did work:

  function dashboardcontroller($scope, $timeout, $uibmodal, dashboardservice, syncservice) {     var vm = this;      // versions     vm.versions = {};      // health      vm.syncservice = syncservice;       $scope.$watch('vm.syncservice.health', function(newhealth, oldhealth){         vm.health = newhealth;      })      // jump-start     activate();      //////////////      function activate(){        getversions();     }      function getversions(){         dashboardservice.getversions()                      .then(function(data) {                         vm.versions = data;                         return vm.versions;                      });     }  } 

i linked similar question in comments , in answers talk using $broadcast if need more 1 controller aware of changes variable's value in service.


Comments