Parag Ghadge
Parag Ghadge

Reputation: 432

AngularJS : Broadcast event not working from app.js in .run method?

.run(['$ionicPlatform', '$rootScope', function($ionicPlatform, $rootScope) {

    $ionicPlatform.ready(function() {

        var PassData= "Test message Data for List";
        $rootScope.$broadcast('dataPassed', PassData);
})
}])

Here in above code broadcast is not getting triggered

In other JS file

 $scope.$on('dataPassed', function(event, args) {
     $scope.message = args;
     console.log('notificationReceived on List : ' + $scope.message);
 });

Here in above code we listen for broadcast for getting triggered which is not working here

Upvotes: 0

Views: 394

Answers (1)

Parag Ghadge
Parag Ghadge

Reputation: 432

I got the answer as, we can listen to broadcast event which is broadcast from .run method only on .run method of other JS file. like as shown below,

.run(['$rootScope', 'DataSharingService', function($rootScope, DataSharingService) {

    $rootScope.$on('notificationReceived', function(event, args) {
        $rootScope.message = args;
        console.log('notificationReceived on List : ' + $rootScope.message);
    });
}]); 

from here you can pass data in any services and so on..

Upvotes: 2

Related Questions