Fairway Technologies Blog


More Angular Routing Topics

In Development No Comments

In the last blog post (Introduction to Angular Routing) you were introduced to routing in Angular (v1.x). You learned the basics of how to route to different HTML fragments using the routing features of Angular. In this blog post you learn more things you can do with routing such as specifying the controller to use, aliasing the controller using “as”, passing parameters to a page and programmatically calling a route.

Set the Controller Name

If the target page for your route has some functionality that needs to be performed, you will have a controller associated with that page. An additional property you can set on the route object when configuring your routes is the controller property. Set this property to the name of the controller function for that page.

<pre>.when('/page2/',<br>{<br>&nbsp; templateUrl: 'page2.template.html',<br>&nbsp; controller: 'Page2Controller'<br>})</pre>

When using the controller property in your route, you must include the .js file that contains the code for that controller on your main HTML page.

<pre>&lt;script src="app.module.js"&gt;&lt;/script&gt;<br>&lt;script src="index.controller.js"&gt;&lt;/script&gt;<br>&lt;script src="page2.controller.js"&gt;&lt;/script&gt;</pre>

Below is the code for the Page2Controller. This controller is a simple sample that just sets a message property with some hard-coded text using the $scope application object.

<div><pre>(function () {<br>&nbsp; 'use strict';<br>&nbsp;&nbsp;<br>&nbsp; angular.module('app')<br>&nbsp; &nbsp;.controller('Page2Controller', Page2Controller);<br>&nbsp; function Page2Controller($scope) {<br>&nbsp; &nbsp; $scope.message = 'Page 2 says \'Hello\'';<br>&nbsp; }<br>})();</pre></div>

Once the message property is set on the $scope object, that value can be displayed on the HTML fragment using the data binding syntax.


Alias the Controller

When developing Angular pages, it is always a good idea to alias your controller using the “as” syntax as shown in the following code snippet.
<div><pre>&lt;div ng-app="app"<br>&nbsp; &nbsp; &nbsp; ng-controller="IndexController as vm"</pre></div>
However, this syntax is only used when you are setting the ng-controller directive on your main page. On your page fragments you are not using this directive. Not to worry, the route object supports a controllerAs property so you can set this alias name.
<div><pre>.when('/page1',<br>{<br>&nbsp; templateUrl: 'page1.template.html',<br>&nbsp; controllerAs: 'page1',<br>&nbsp; controller: 'Page1Controller'<br>})<br>.when('/page2',<br>{<br>&nbsp; templateUrl: 'page2.template.html',<br>&nbsp; controllerAs: 'page2',<br>&nbsp; controller: 'Page2Controller'<br>})</pre></div>
When you use the "as" syntax, the application scope is passed into your controller as ‘this’. Assign 'this' to a local variable in your controller, then create any properties you want on the application scope so you can use those properties in your HTML. In the Page1Controller, shown below, create a property named message and set it to a hard-coded string.
<div><pre>(function () {<br>&nbsp; 'use strict';<br>&nbsp;&nbsp;<br>&nbsp; angular.module('app')<br>&nbsp; &nbsp;.controller('Page1Controller', Page1Controller);<br>&nbsp; function Page1Controller() {<br>&nbsp; &nbsp; var vm = this;<br>&nbsp; &nbsp; vm.message = 'Hello from Page One!';<br>&nbsp; }<br>})();</pre></div>
To use the message property on your HTML page, prefix the property name with the value you set in the controllerAs property. To display the value in the message property in your page1.template.html page you use the following code.
Again, if you are specifying controllers in your routing, make sure you include the appropriate .js files on your index.html page.
<div><pre>&lt;script src="app.module.js"&gt;&lt;/script&gt;<br>&lt;script src="index.controller.js"&gt;&lt;/script&gt;<br>&lt;script src="page1.controller.js"&gt;&lt;/script&gt;<br>&lt;script src="page2.controller.js"&gt;&lt;/script&gt;</pre></div>

Pass a Parameter to a Route

Sometimes you need to pass a parameter to a page. You can also pass parameters to a route. In the anchor tag shown below, you are passing a value of 12345 to the route defined by /page2.
<div><pre>&lt;a href="#/page2/12345"&nbsp;<br>&nbsp; &nbsp;class="btn btn-primary"&gt;<br>&nbsp; Page 2 with parameter<br>&lt;/a&gt;</pre></div>
Keep your original /page2 route, but add an additional route using a when() function. After the /page2 add another forward slash followed by a colon (:) and a variable name such as id.
<div><pre>.when('/page2',<br>{<br>&nbsp; templateUrl: 'page2.template.html',<br>&nbsp; controllerAs: 'page2',<br>&nbsp; controller: 'Page2Controller'<br>})<br>.when('/page2/:id',<br>{<br>&nbsp; templateUrl: 'page2.template.html',<br>&nbsp; controllerAs: 'page2',<br>&nbsp; controller: 'Page2Controller'<br>})</pre></div>
In your Page2Controller function, you now need to have the $routeParams injected. Add the $routeParams service as a parameter to your Page2Controller function. Before you use the parameter, test to see if the value was passed in by using an if statement as shown in the following code.
<div><pre>function Page2Controller($routeParams) {<br>&nbsp; var vm = this;<br>&nbsp; if ($routeParams.id) {<br>&nbsp; &nbsp; vm.message = 'The id passed in was: '<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+ $routeParams.id;<br>&nbsp; }<br>&nbsp; else {<br>&nbsp; &nbsp; vm.message = 'Page 2 says \'Hello\'';<br>&nbsp; }<br>}</pre></div>
If you need more than one value passed, just add another route such as the following:
<div><pre>.when('/page2/:id/:extraText',<br>{<br>&nbsp; templateUrl: 'page2.template.html',<br>&nbsp; controllerAs: 'page2',<br>&nbsp; controller: 'Page2Controller'<br>})</pre></div>
In your index.html, you call this route using the following:
<div><pre>&lt;a href="#/page2/123/abc"&nbsp;<br>&nbsp; &nbsp;class="btn btn-primary"&gt;<br>&nbsp; Page 2 with 2 parameters<br>&lt;/a&gt;</pre></div>
In the controller for this page, add another test for this additional parameter.
<div><pre>if ($routeParams.extraText) {<br>&nbsp; vm.message = 'extraText=' + $routeParams.extraText;<br>}</pre></div>

Programmatically Calling a Route

Many times, when running code in one of your controllers, you will have a need to redirect the user to a new route. This is accomplished by calling the path() function on the $location service and then passing in one of your defined routes. The following code shows a couple of functions you may have in a controller. If either of these functions are called, the URL of the browser is set to the new path, and then the user is directed to the appropriate page. Make sure you add the $location service as a parameter to your controller function.
<div><pre>function IndexController($location) {<br>&nbsp; function goToPage1() {<br>&nbsp; &nbsp; $location.path("/page1");<br>&nbsp; }<br>&nbsp; function goToPage2() {<br>&nbsp; &nbsp; $location.path("/page2");<br>&nbsp; }<br>}</pre></div>


In this blog post you learned how to route to page fragments, but specify the controller to use with that page. The controller and controllerAs properties of the route object as used to set the information for the controller to be used with a page. You are able to add one or more parameters to a route. The $routeParams service can retrieve as many different parameters passed to a controller as you want. Use the $location service to programmatically direct a user to a route in your Angular application.

Sample Code

You can download the code for this sample at www.pdsa.com/downloads. Choose the category “PDSA Blog”, then locate the sample PDSA Blog Sample: More Angular Routing Topics.
Plan Your Technology Team with This Online Assessment Tool

Subscribe to Email Updates

Build a Technology Dream Team. Start with This Online Assessment. →