Posts

Showing posts with the label Broadcast

AngularJS $broadcast With Multiple Parameters

Answer : Just put the parameters into an object: $scope.$broadcast('event', { a: item1, b: item2 }) Then access them from the second argument to the callback: $scope.$on('event', function(event, opt) { // access opt.a, opt.b }); Or if using ES2015 syntax you can unpack the arguments: $scope.$on('event', (event, {a,b}) => { // access them just as a, b }); Documentation says: 'Optional one or more arguments which will be passed onto the event listeners' $rootScope.$emit(event_name, p1, p2, p3);

AngularJS $on Event Handler Trigger Order

Answer : Very good question. Event handlers are executed in order of initialization. I haven't really thought about this before, because my handlers never needed to know which one run first, but by the look of you fiddle I can see that the handlers are called in the same order in which they are initialized. In you fiddle you have a controller controllerA which depends on two services, ServiceA and ServiceB : myModule .controller('ControllerA', [ '$scope', '$rootScope', 'ServiceA', 'ServiceB', function($scope, $rootScope, ServiceA, ServiceB) {...} ] ); Both services and the controller define an event listener. Now, all dependencies need to be resolved before being injected, which means that both services will be initialized before being injected into the controller. Thus, handlers defined in the services will be called first, because service factories are initialized before contro...