Wildcards for listening to broadcast using $ on on AngularJs

Is there a way to capture area translations using wildcards in AngularJS?

Example:

$rootScope.$on('*created', function () { // do stuff });

+8
angularjs
source share
2 answers

In the source code, angular js $on is defined as follows:

 $on: function(name, listener) { var namedListeners = this.$$listeners[name]; if (!namedListeners) { this.$$listeners[name] = namedListeners = []; } namedListeners.push(listener); return function() { namedListeners[indexOf(namedListeners, listener)] = null; }; }, 

since this.$$listeners is an associative array, and associative arrays in javascript do not accept regular expressions as keys, this suggests that the short answer is "no, you cannot."

+18
source share

Not out of the box, but .. You can create a customOn () helper function and use it instead of $ on. customOn () can execute a regular expression for the name of the event and send any event.

0
source share

All Articles