Gaurav
Gaurav

Reputation: 123

On event executed multiple times after compilation

I have this directive:

    myApp.directive('someDir', function($compile){
    return{
        restrict: 'A',
        link: function(scope, element, attrs){
            scope.$on("compileTask", function() {
                $compile(element)(scope);
            });
        }
    }
});

And in my controller I am broadcasting the event as:

myApp.controller('GreetingController', ['$scope', function($scope) {
  $scope.$broadcast('compileTask');
}]);

And the view is:

<h2 some-dir ng-bind-html="employee.name"></h2>
<h4 some-dir ng-bind-html="employee.designation"></h4>

The problem is that, when the 'compileTask' event is broadcasted, It is broadcasted multiple times. i want to know why it is broadcasting the event multiple times? And what is the way to allow broadcast for single time

Upvotes: 3

Views: 199

Answers (1)

rmg.n3t
rmg.n3t

Reputation: 987

The event is 'broadcasted' to all event listeners. Since you have two directives listening for the event, they will both handle it.

Upvotes: 1

Related Questions