Broadcast is executed once but the event is called 2 times

1

I have the following "broadcast" inside a "directive":

console.log('a emitir...');
$rootScope.$broadcast('olamundo', params);

In my controller I have the following code:

$scope.$on('olamundo', function (event, args) {
    console.log('a escutar...');
    //aqui vai o código
});

My "olamundo" event is running 2 times, the phrase "listening ..." appears twice in the console (due to "console.log" in the code above). In the "directive" where the "broadcast" is issued, the phrase 'to issue ...' appears feathers once. Is there no other broadcast with the same name, because the 'olamundo' event is running 2 times?

    
asked by anonymous 05.03.2015 / 21:07

1 answer

2

This may be indicative that you actually have two instances of your controller .

This can occur if you are mentioning the controller as part of the configuration of ui-router (or $stateProvider , if you are using ui-router ) and also in the HTML document.

If this is the case, remove one of the two (the configuration of ng-router / ui-router or the mention in HTML.)

    
05.03.2015 / 21:36