16.07.2017 Views

AngularJS Essentials

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 4<br />

There are three events that can be broadcasted by the $route service and are very<br />

useful in many situations. The broadcasting mechanism will be studied in the next<br />

chapter, Chapter 5, Scope.<br />

The first event is the $routeChangeStart event. It will be sent when the routing<br />

process starts and can be used to create a loading flag, as follows:<br />

run.js<br />

parking.run(function ($rootScope) {<br />

$rootScope.$on("$routeChangeStart", function(event, current,<br />

previous, rejection)) {<br />

$rootScope.loading = true;<br />

});<br />

});<br />

After this, if all the promises are resolved, the $routeChangeSuccess event is<br />

broadcasted, indicating that the routing process finished successfully:<br />

run.js<br />

parking.run(function ($rootScope) {<br />

$rootScope.$on("$routeChangeSuccess", function(event, current,<br />

previous, rejection)) {<br />

$rootScope.loading = false;<br />

});<br />

});<br />

If any of the promises are rejected, the $routeChangeError event is broadcasted,<br />

as follows:<br />

run.js<br />

parking.run(function ($rootScope, $window) {<br />

$rootScope.$on("$routeChangeError", function(event, current,<br />

previous, rejection) {<br />

$window.location.href = "error.html";<br />

});<br />

});<br />

[ 95 ]<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!