0

I have a piece of code that needs to be invoked after execution of some other code. i'm using broadcast for this. I need to invoke the same code from 2 different places. Now that is not working for me. My code is

Broadcast 1

scope.$broadcast('selOrdLine', ordline);

On

scope.$on('selOrdLine', function (event, args) {
 // some code here
})

I need to invoke this code from some other place also

so I put scope.$broadcast('selOrdLine', ordline); there also. But it is returning error.

Edit

The second time when I use broadcast I'm making the ordline data using the angular.forEach loop. so I'm using $q for running the broadcast only after the data is ready like this

$q.all(orderline).then(function () {
    $scope.$broadcast('selOrdLine', orderline);
});

The error is, the broadcast is invoked multiple times. But as I use $q it should run only after the orderline data is set, right ?

I'm nidhin
  • 2,592
  • 6
  • 36
  • 62
  • 1
    There is no such restriction from angular than you can't fire same event from two places. I think the error is for something else. Can you put the error message here? – Indranil Mondal Jul 07 '16 at 16:53
  • What error are you getting? – nweg Jul 07 '16 at 16:54
  • 1
    You probably want $rootScope.$broadcast instead of scope.$broadcast – jbrown Jul 07 '16 at 16:58
  • It might be some orther error. broadcast works multiple time in multiple places – Aniruddha Das Jul 07 '16 at 16:59
  • You may want to read this fine [answer about event firing and handling](http://stackoverflow.com/questions/26752030/rootscope-broadcast-vs-scope-emit). It may help you to figure it out by yourself. – Italo Ayres Jul 07 '16 at 17:45

0 Answers0