abelski
abelski

Reputation: 383

how to fetch an event from one separate controller to another page controller?

We've tried to fetch the broadcast event from a page controller to another using this code.

Controller1.js
    scope.createList = function() {
        scope.$broadcast("event: list updated", true);
    }

Controller2.js
   scope.$on("event: list updated", function(value) {
      console.log('event update");
   });

This method is working within one page controller, but not in separate js controllers. How can the controller2.js be notified by the controller1.js?

Upvotes: 0

Views: 68

Answers (1)

$broadcast method emits events for all children scopes, so listener controller should be among them. Instead of broadcasting on scope, you can do it on $rootScope instead, so every scope will be able to catch the event.

.controller("Controller1", function ($scope, $rootScope) {
    $scope.createList = function() {
        $rootScope.$broadcast("event: list updated", true);
    }
})

Upvotes: 1

Related Questions