[FIXED] I am getting issue in broadcasting and listening event

Issue

First.js

scope.menuItemClick = function (def, callbackText, keepMenuOpen) {

  console.log(def);

  if(def.automationId === "Print"){

  console.log("before broadcast");

  $scope.$root.$broadcast("printingForms","my Data");

  console.log("after broadcast");

}

Second.js

$scope.$on("printingForms",(event, data) => {

  console.log(data);

  console.log("listening");

});

I am not able to listen the broadcast event though I am broadcasting it from root. can anyone help me?
Thanks…..

Solution

You should use $rootScope to broadcast events. Just inject $rootScope on your controller as same as $scope

Controller 1

$rootScope.$broadcast("printingForms","my Data");

Controller 2

$rootScope.$on("printingForms",function(event, data){
    console.log(data);
});

Answered By – Thusitha

Answer Checked By – Marilyn (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published