Search code examples
angularjsipadmobile-devices

how to make $scope.$broadcast work in Ipad?


I have two controllers, say controller1 and controller2. I have function called function1 defined in controller1. When I try to call function1 from controller2 using $scope.$broadcast, it works fine in desktop browsers.

But it does not enter into $scope.$on which is written in controller1 in Ipad browser chrome.

I have tried different ways for this to work. importing controller1 into controller2 and so on. But then variables in the browser does not reflect its values.

$scope.$on in controller1:

$scope.$on("con1function", function(event, activity, action) {

    $scope.function1(activity,'dashboard');
})

Controller2:

$scope.con2function = function() {
    $scope.$broadcast("con1function",data[0], '');
}

can anyone tell me what is wrong here? Or what is the way in which I can make this work for the Ipad as well.


Solution

  • You can try $rootScope.$broadcast("con1function","Hi there") to broadcast the message and $scope.$on() to recieve it.

    If your controllers are not related (as parent or child) then just $scope.broadcast or $scope.emit may not work.

     var myApp = angular.module('myApp',[]);
     myApp.controller('controller1', function ($scope,$rootScope) {
         $scope.sendMessage = function(){
          console.log("in sendMessage");
          $rootScope.$broadcast("con1function","Hi there")
         }
        });
      myApp.controller('controller2', function ($scope,$rootScope) {
          console.log("in controller2");
          $scope.$on("con1function", function(event, activity) {
            console.log('dashboard',activity);
          });
        });
    

    Also make sure that required controllers are loaded in your script or HTML.