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 />

Creating a single-page application<br />

In the past few years, the single-page application, also known as SPA, has been<br />

growing in popularity among frontend developers. It improves customers' experiences<br />

by not requiring the page to be constantly reloaded, taking advantage of technologies<br />

such as AJAX and massive DOM manipulation.<br />

Installing the module<br />

<strong>AngularJS</strong> supports this feature through the $route service. Basically, this service<br />

works by mapping URLs against controllers and views, also allowing parameter<br />

passing. This service is part of the ngRoute module and we need to declare it before<br />

using it, as follows:<br />

index.html<br />

<br />

After this, the module should be imported to the parking module:<br />

app.js<br />

var parking = angular.module("parking", ["ngRoute"]);<br />

Configuring the routes<br />

With the $routeProvider function, we are able to configure the routing<br />

mechanism of our application. This can be done by adding each route through the<br />

when function, which maps the URL pattern to a configuration object. This object has<br />

the following information:<br />

• controller: This is the name of the controller that should be associated with<br />

the template<br />

• templateUrl: This is the URL of the template that will be rendered by the<br />

ngView module<br />

• resolve: This is the map of dependencies that should be resolved and<br />

injected inside the controller (optional)<br />

• redirectTo: This is the redirected location<br />

[ 87 ]<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!