Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS $broadcast with multiple parameters

Can I have a $broadcast $on with multiple parameters, something like:

$scope.$broadcast('event',$scope.item, $scope.item); 

Is it possible to have something like this or something similar in any case ?

like image 896
blaa Avatar asked Jul 06 '16 07:07

blaa


1 Answers

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 }); 
like image 152
Duncan Avatar answered Sep 30 '22 14:09

Duncan