I was told that using angular events can be expensive (I could not verify this)
Any calls $broadcastand $onshould be βwrappedβ using a factory or service to enter their respective components to maintain performance?
$broadcast
$on
Again, I would rather use $onand listen directly to the events, rather than creating a factory that essentially just registers the functions to be called when it receives the event - lets call it a dispatcher.
Please note that this is not only one component (directives) that listens to "some event", there will be many components that listen to this event.
Manager example:
angular.module('app').factory('dispatcher', ['$rootScope', function ($rootScope) { var registeredFns = [ ]; $rootScope.$on('some-event', function (evt, msg) { _.each(registeredFns, function (fn) { fn.apply(null, msg); }); }); return { onSomeEvent: function (fn) { registeredFns.push(fn); } }; });
, - , , , , .
... , ( - !)
, , . $emit(), for() . , , "" , $rootScope.
for()
, stopPropagation, .
$broadcast - , : (), child. , , $rootScope.$broadcast(), ngRepeats , , , , , Angular , .
$rootScope.$broadcast()
, jsPerf , $rootScope.$emit() $rootScope.$on(). , .
$rootScope.$emit()
$rootScope.$on()