user5760980
user5760980

Reputation:

Angular $broadcast does not work

When I try to broadcast an event:

$scope.selectCategory = function(cat) {
    $scope.$broadcast('Cat', {catID: cat});
}

it doesn't get caught by $scope.$on:

$scope.$on('Cat', function (event, args) {
    console.log("in");
    $scope.catid = args.catID;
});

Question: I successfully pass the variable into broadcast but $scope.on callback never gets triggered.

Upvotes: 0

Views: 58

Answers (1)

rupampatel
rupampatel

Reputation: 300

you need to $broadcast on $rootScope:

$scope.selectCategory = function(cat) {
                                // $location.path('/films/' + cat);
                                $rootScope.$broadcast('Cat', {catID: cat});
                                console.log(cat);
                            }

rest of your code should be fine then.

Ref: Official Angular Guide

Upvotes: 1

Related Questions