2

I am unable to broadcast to other controllers as I have no parent child relation ship therefore, using $rootScope to broadcast the change to other controllers.

Please help me to identify the problem in code.

function serverController( server, $state, $rootScope, $timeout)
{
    var vm  = this;
    vm.loader = false;

    (function tick() {
        server.setRootScope().then(
            function(response){
                angular.forEach(response.data, function (val) {
                    val['serverState'] = (val.status == 'running')?true:false;
                    $rootScope.servers[val.id] = val;
                });
                vm.servers = $rootScope.servers;
                $rootScope.$broadcast('serverUpdated', [1,2,3]);
                console.log('serverUpdated', [1,2,3]);
            }
        );

        $timeout(tick, 25000);
    })();
}


function serverManageController(server, $state, $rootScope, $stateParams)
{
    var vm  = this;
    $rootScope.$on('serverUpdated', function(event, mass) {
        console.log('serverUpdated');
        console.log(mass);
    });
}
Sam
  • 376
  • 3
  • 10
  • While broadcasting through scope works, I would also ask you to check out services for controller to controller talk. Also, $scope.$on('serverUpdated' would be a better choice then $rootScope.$on('serverUpdated' I think. – TheRodeo Nov 23 '15 at 07:38

1 Answers1

1

Modify tick function like this.

 function tick() {
    server.setRootScope().then(
        function(response){
            angular.forEach(response.data, function (val) {
                val['serverState'] = (val.status == 'running')?true:false;
                $rootScope.servers[val.id] = val;
            });
            vm.servers = $rootScope.servers;
            $rootScope.$broadcast('serverUpdated', [1,2,3]);
            console.log('serverUpdated', [1,2,3]);
        }
     );
  };

 $timeout(function(){
   $interval(tick, 25000);
 },2000);

and it should work as you expect.

Ashot
  • 1,229
  • 1
  • 12
  • 13
  • tick() is the poling function it is executed after specified time automatically, if I place this outside then poling is stopped. – Sam Nov 23 '15 at 07:42
  • If it so you should use $interval service . I'll modify the code now. – Ashot Nov 23 '15 at 07:43
  • Edited and do not forget to inject $interval service in you controller as well. – Ashot Nov 23 '15 at 07:45
  • and it will be best practice to wrap whole polling logic in one service with specific methods start,stop,setInterval etc. – Ashot Nov 23 '15 at 07:46
  • interval works but still the broadcast is not working. serverManageController is not listening, I think serverManageController is not instantiated at the time of broadcasting. – Sam Nov 23 '15 at 07:49
  • If it so .. it should fail only once . isn't it ? Because tick function will be called immediately after page refresh and after each 25 milliseconds . So you need wrap $interval into $timeout in order to let serverManageController to instantiate first time. – Ashot Nov 23 '15 at 08:02
  • I have modified an answer. Try that. – Ashot Nov 23 '15 at 08:06
  • Still serverManageController is not getting the broadcast – Sam Nov 23 '15 at 08:15
  • Are you sure you response from server is coming successful ? Maybe you are missing error callback ? Maybe your controller is not attached on any html element. Check these reasons please. – Ashot Nov 23 '15 at 08:23
  • response from server is coming, i have consoled its response. serverManageController is attached to a template and its rendering perfectly, but not updating itself on broadcast of tick() function. – Sam Nov 23 '15 at 08:28
  • you may hard-code the response and if you want to test. – Sam Nov 23 '15 at 08:28
  • Could you add your code in plunker so I can review it.? – Ashot Nov 23 '15 at 08:29
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/95898/discussion-between-ashot-and-sam). – Ashot Nov 23 '15 at 08:31
  • Thank You so much Ashot for your help and guidance, But I have solved the issue by using https://github.com/bdadam/PubSub and works like charm. – Sam Nov 23 '15 at 10:33
  • There are many fall-backs in angular builtin broadcast, emit, on it fails on different browsers and do not support different tabs support. Contextual bindings was the main issue. – Sam Nov 23 '15 at 10:35
  • All clear . There is also PubNub I used it for one project .Thank you too @Sam – Ashot Nov 23 '15 at 20:45