Mastering Web Application

(Rick Simeone) #1
Chapter 1

While AngularJS event system is modeled after the DOM one, both event
propagation systems are totally independent and have got no common
parts.

While events propagated through the scopes' hierarchy are very elegant solutions
to several problems (especially when it comes to notifications related to global,
asynchronous state changes), those should be used sparingly. Usually we can rely on
the two-way data binding to end up with a cleaner solution. In the entire AngularJS
framework, there are only three events being emitted ($includeContentRequested,
$includeContentLoaded, $viewContentLoaded), and seven events being
broadcasted ($locationChangeStart, $locationChangeSuccess, $routeUpdate,
$routeChangeStart, $routeChangeSuccess, $routeChangeError, $destroy). As
you can see, scope events are used very sparingly and we should evaluate other
options (mostly the two-way data binding) before sending custom events.


Don't try to mimic the DOM event-based programming model in
AngularJS. Most of the time there are better ways of structuring your
application, and you can go very far with the two-way data binding.

Scopes lifecycle


Scopes are necessary to provide isolated namespaces and avoid variable name
collisions. Scopes which are smaller and organized in a hierarchy help in managing
memory usage. When one of the scopes is no longer needed, it can be destroyed.
As a result, model and functionality exposed on this scope will be eligible for
garbage collection.


New scopes are usually brought to life and destroyed by the scope-creating
directives. It is also possible to manually create and destroy scopes by calling
the $new() and $destroy() methods, respectively (both methods are defined
on the Scope type).


View

We've seen enough examples of AngularJS templates to realize that it is not
yet another templating language, but quite a different beast. Not only does the
framework rely on the HTML for its template syntax and allow us to extend the
HTML vocabulary, but it has the unique ability to refresh parts of the screen
without any manual intervention!

Free download pdf