angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

Register multiple events with Scope.$on

Open lcanet opened this issue 13 years ago • 52 comments

Be able to bind a listener to multiple events with one call

$scope.$on(['sizeChanged', 'visibilityChanged'], myListener);

The returned deregistration function should remove both of course


PRs: #4492 and #7761

lcanet avatar Oct 15 '13 14:10 lcanet

I would argue that not having this feature adds yet another gotcha to angular for everyone who have used jquery or other popular event libraries (such as Bean.js). This idiom has been used for years so most programmers are expecting $on('event1 event2') to work.

phzbox avatar Nov 22 '14 23:11 phzbox

Food for thought Not sure where the statistics are coming from for the number of projects needing this feature vs those that don't. Just in case it's a gut feel statistic, I'd like to add that I've needed this on more projects than not.

Usually, if my app needs events (most do), then it tends to need a fair number of them. With a fair number of events, I tend to need to fire something on more than one event. It is more rare that I have the need for events and not the need to fire the same action on multiple events at least once.

That said, I appreciate the push to keep the library small. So, thanks for that, too.

DRY Workaround Credit this working jsFiddle

angular.forEach(['one', 'two', 'three'], function(value) {
  $scope.$on(value, function(event) {
    console.log(event.name);
  });
});

levithomason avatar Dec 15 '14 20:12 levithomason

+1

hmbilal avatar Dec 16 '14 10:12 hmbilal

@levithomason The problem with that is you are not cleaning it up.. I ended up doing this which works great:

https://github.com/exceptionless/Exceptionless.UI/blob/master/src/components/refresh-on/refresh-on-directive.js

niemyjski avatar Dec 16 '14 14:12 niemyjski

It would be super nice to do on 'xyz abc' and then when you call the returned function it cleans them up.

niemyjski avatar Dec 16 '14 14:12 niemyjski

I agree that this is a pain point that should be addressed. I expected it to be in the library and was extremely surprised to find that it wasn't.

bradrogers2 avatar Apr 06 '15 21:04 bradrogers2

+1

mrbinky3000 avatar Apr 29 '15 14:04 mrbinky3000

Just got bitten by this.

cesarvarela avatar May 15 '15 18:05 cesarvarela

+1

sSeewald avatar Jun 07 '15 07:06 sSeewald

+1

 On Sunday, June 7, 2015 3:52 AM, Sascha Seewald <[email protected]> wrote:

+1— Reply to this email directly or view it on GitHub.

rishi-tandon avatar Jun 07 '15 14:06 rishi-tandon

+1

rserro avatar Jul 09 '15 14:07 rserro

+1

pwhe23 avatar Jul 19 '15 04:07 pwhe23