Events, life cycle, and scoping

531 views
Skip to first unread message

Joshua Gruber

unread,
Feb 17, 2012, 12:11:25 PM2/17/12
to AngularJS
I'm looking to understand a few AngularJS concepts more deeply and
haven't yet found them in the documentation. The core question is
"What events get emitted as Angular does its work in initialization,
compiling templates, and opening for business?" followed by the same
question for the life cycle of a widget, view, etc. Both when routed
in and routed out of.

Is there a document that describes these events and their order?
If not, is there a document or section of code that at least lists
them?
If not, is there a way to use $on to listen for ALL events so I can
pipe them to the console? If so, what would be the best way to attach
the listener as early in the life cycle as possible?
If not... maybe I can find all of the events with a little grepping,
but I still would love help getting that last question answered:
what's the earliest point you can attach a listener to the global
scope, and how would one go about getting a function attached there?

Vojta Jina

unread,
Feb 18, 2012, 3:19:15 PM2/18/12
to ang...@googlegroups.com
We don't use events that much at this moment. Basically only $route fires some events ($beforeRouteChange, $afterRouteChange, $routeUpdate): http://docs-next.angularjs.org/api/angular.module.ng.$route
And form widgets emits some events as well ($invalid, $valid, $viewChange, ...)

There is no way to listen on all events, but you can monkey patch the scope prototype to see all the events... 
var app = angular.module('myApp', []);
app.run(function($rootScope) {
  var $emit = $rootScope.__proto__.$emit;
  $rootScope.__proto__.$emit = function(eventName) {
    console.log('EVENT ' + eventName);
    return $emit.apply(this, arguments);
  };
});


V.

Joshua Gruber

unread,
Feb 18, 2012, 7:34:43 PM2/18/12
to AngularJS
Thanks for both the detail and the tricks. I think between the route
events (for transition animation) and $filter (was looking to make an
i18n pass that should have been done The AngularJS Way) my needs are
actually met. Any events I need for widgets I can (and should) wire
up myself.
Reply all
Reply to author
Forward
0 new messages