Angular Routing
Objectives
Incorporate an organized folder structure into an Angular app
Use a configuration block to add routing to an app
Retrieve route params from within a controller
Compare hashbang mode to HTML5 mode when configuring the router
Note, we'll be building upon the Star Wars app from the Angular Services codealong
Refactoring the Star Wars App
You'll probably notice soon that placing all of our controllers and services into app.js
will soon become disorganized. So let's refactor the Star Wars app from earlier. Let's follow this layout.
This will work for our examples in class, but for more complex apps, you will want to look at file structures that compartmentalize your app even more. See this Scotch tutorial for details
Now, let's move everything out of app.js
so that we have three modules. We'll be injecting these modules like so:
inject ngResource into StarWarsServices
inject StarWarsServices into StarWarsCtrls
inject StarWarsCtrls into StarWarsApp
Whew! That's a lot of dependency injection. While beyond the scope of our Angular unit, injecting dependencies makes testing in Angular easier, because we can mock different dependencies if needed by defining test dependencies (this would be very useful for "faking" API calls).
app.js
controllers.js
services.js
Also, make sure to change and include those script files in index.html
Moving on, let's add routing to our app.
Using the AngularUI Router
We'll be using the popular ui.router
module for routing our application.
The CDN link you'll need in index.html
:
You'll probably notice that this is a dependency, and it'll need to be injected into our app.
In app/app.js
:
And lastly, we'll need a way to switch out templates and controllers on our index page. For now, let's replace the contents of <body></body>
with the following:
In index.html
:
The ui-view
directive will help complement our routes by including the template and controller of the current route. Whenever a route changes, the contents of ui-view
are replaced with the new template and controller.
But we need routes! And controllers! And views! We already have the controller, so let's set up the view. In app/views
, we'll create a template called films.html
to list out all the films.
`app/views/films.html
Configuring Routes
In app/app.js
, let's add the following to the bottom of the file:
Note: Run the app using a local HTTP server.
To add additional routes, just add additional state
functions. Remember to define a controller (if necessary) for each page and create content in the related template files. Adding the otherwise
condition is handy for redirecting invalid routes.
Try it: Take a moment and implement a template for an about page.
Linking to routes
You can link to angular routes using <a>
link tags
Wait, what's this prefix?
You'll see that Angular routes contain a "hashbang" prefix #. This is necessary in order to distinguish between front-end and back-end routes. A route like /about
would end up being routed to a server, while Angular knows that /#/about
is a front-end route.
We can configure our app to not use the "hashbang", but it will involve some configuration on the server. Specifically for Express, you would want all routes that aren't the root route (/
) to send the back index.html
.
See this StackOverflow response for more on this topic. We'll be implementing this configuration later on.
Passing URL Parameters
Passing URL parameters with angular routes is similar to routes in Express or Rails. First we define a route with a parameter.
Then, we can access the parameter in the controller using $stateParams
(which must be injected into the controller). So let's add another controller in controllers.js
for getting a specific film.
Now we need a filmShow.html
file in the views
folder:
...andddddd we need to add links to views/films.html
Note: Why do we use ng-href
? Because Angular (see documentation)
Programatically Navigating
You can also cause the angular router to navigate to a new route using $state
(another service).
This can also be used to get the current state, simply by leaving the parenthesis empty.
Resources
The official Angular router, with less functionality than UI router
Last updated