goncalomarques
goncalomarques

Reputation: 85

Reaching controller scope on $rootScope.$on inside controller

How can I reach the $scope and modify it from inside my $rootScope.$on event handler?

angular.module('Foo')
        .controller('BarCtrl', ['$scope', '$rootScope', function ($scope, $rootScope) {
            $scope.x = {};
            $rootScope.$on('myEvent', function(event, data){
                if(data){
                    //how to reach BarCtrl $scope.x here?
                }else{
                    //same here
                }
            })
        }])

Upvotes: 0

Views: 114

Answers (1)

v1vendi
v1vendi

Reputation: 613

simply use $scope variable

$rootScope.$on('myEvent', function(event, data){
    $scope.x = data;
});

Upvotes: 1

Related Questions