Tuesday, 12 April 2016

Understanding AngularJS Routing

Routing is a mechanism which helps you to divide your single page application into multiple views and bind these views to corresponding controllers. In this article, you will learn how to configure angular routing.

An angular route is specified within the URL by using # sign and enables you to show a specific view. Some example of angular routes are given below:

  1. http://yourdomain.com/index.html#users

  2. http://yourdomain.com/index.html#orders

  3. http://yourdomain.com/index.html#books

  4. http://yourdomain.com/index.html#games

AngularJS Route Module


The routing functionality is provided by the angular's ngRoute module, which is the part of angular-route.jsJavaScript file. Hence, include angular-route.js file into your AngularJS application for using routing. You have to add ngRoute module as an dependent module as given below:

  1. var app = angular.module("app", ['ngRoute']);

Configuring the $routeProvider


The routes in your angular application are declared with the help of $routeProvider which is the provider of the $route service. The $routeProvider is configured in your app module's config() function.

Syntax to configure Routing




  1. appmodule.config(['$routeProvider',

  2. function($routeProvider) {

  3. $routeProvider.

  4. when('/route1', {

  5. templateUrl: 'template-1.html',

  6. controller: 'RouteController1'

  7. }).

  8. when('/route2:param', {

  9. templateUrl: 'template-2.html',

  10. controller: 'RouteController2'

  11. }).

  12. otherwise({ // if no route paths matches

  13. redirectTo: '/'

  14. });

  15. }]);


  16.  

  17. <!-- And links should be defined as: -->

  18.  

  19. <a href="#/route1">Route 1</a><br/>

  20. <a href="#/route2:123">Route 2</a><br/>

The when() function takes a route path and a JavaScript object as parameters. The route path is matched against the part of the URL after the # sign. The otherwise() function redirects to the specified route if no route paths matches.

The ngView Directive


The ngView directive renders the template of the current route into the main layout (index.html) file. Every time, when the current route changes, the ngView directive view changes based on the new route path settings.


  1. ng-view>

An Example of Routing


index.html



  1. <!DOCTYPE html>

  2. <html>

  3. <head>

  4. <title>AngularJS Routing</title>

  5. src="lib/angular.js">

  6. src="lib/angular-route.js">

  7.  


  8. var app = angular.module("app", ['ngRoute']);

  9.  

  10. app.controller("homeController", function ($scope) {

  11.  

  12. $scope.message = "Welcome to Home Page!";

  13. });

  14.  

  15. app.controller("aboutController", function ($scope) {

  16.  

  17. $scope.message = "Welcome to About Page!";

  18. });

  19.  

  20. app.controller("contactController", function ($scope) {

  21.  

  22. $scope.message = "Welcome to Contact Page!";

  23. });


  24. app.config(['$routeProvider', function ($routeProvider) {

  25. $routeProvider.when("/", {

  26. templateUrl: "templates/pages/home.html",

  27. controller: "homeController"

  28. }).when("/about", {

  29. templateUrl: "templates/pages/about.html",

  30. controller: "aboutController"

  31. }).when("/contact", {

  32. templateUrl: "templates/pages/contact.html",

  33. controller: "contactController"

  34. }).otherwise({

  35. redirectTo: 'index.html'

  36. });

  37. }]);

  38.  


  39. </head>

  40. <body ng-app="app">


  41. href="#">Home |

  42. href="#contact">Contact |

  43. href="#about">About



  44. ng-view>

  45. </body>

  46. </html>

home.html



  1. <h1>Home Page</h1>

  2. <h2>{{message}}</h2>

about.html



  1. <h1>About Page</h1>

  2. <h2>{{message}}</h2>

contact.html



  1. <h1>Contact Page</h1>

  2. <h2>{{message}}</h2>

What do you think?

I hope you will enjoy the AngularJS Routing while developing your app with AngularJS. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

No comments:

Post a Comment