AngularJS Routing



  • The AngularJS Supports Single Page Application (SPA) which shows multiple view templates on a single page.
  • We can use the ngRoute module for navigate the multiple pages without reload the page.
  • To use routing we should include AngularJS "angular-route.js" JavaScript source file from googleapis.com.
  • The following supports are given by AngularJS for routing
    • ngRoute Module.
    • ng-view Directive.
    • $routeProvider Provider.
    • ng-template Directive.

Syntax for ngRoute Module in AngularJS:

angular.module(’myApp’, [‘ngRoute’]);

Sample code for Routing in AngularJS:

 Tryit<!DOCTYPE html>
<html>
    <head>
        <title>Wikitechy AngularJS Tutorials</title>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/
        angular.min.js"> </script>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/
        angular-route.min.js"> </script>
        </head>
        <body ng-app="myApp">
            <p><a href="#/">Tutorials</a></p>
            <a href="#html">HTML</a>
            <a href="#css">CSS</a></p>
            <a href="#php">PHP</a></p>
            <div ng-view></div>          
            <script>
                var app = angular.module("myApp", [“ngRoute”]);
                app.config(function($routeProvider) {                                       
                    $routeProvider
                         .when("/", {
                              templateUrl : "tutorials.html"
                         })
                         .when("/html", {
                              templateUrl : "html.html"
                         })
                         .when("/css", {
                              templateUrl : "css.html"
                         })
                         .when("/php", {
                              templateUrl : "php.html"
                         });
               });
        </script>
    </body>
</html>

tutorials.html template:

<h2>Wikitechy Tutorials</h2>
<p>Welcome to Wikitechy tutorials</p>

html.html template:

<h2>HTML Tutorials</h2>
<p>Welcome to HTML tutorials</p>

css.html template:

<h2>CSS Tutorials</h2>
<p>Welcome to CSS tutorials</p>

php.html template:

<h2>PHP Tutorials</h2>
<p>Welcome to PHP tutorials</p>

HTML:

  • Viewable HTML contents in AngularJS Application.
<p><a href="#/">Tutorials</a></p>
<a href="#html">HTML</a></p>
<a href="#css">CSS</a></p>
<a href="#php">PHP</a></p>
<div ng-view></div>

Routing :

  • Routing configuration of Application Module.
var app = angular.module("myApp", [“ngRoute”]);
app.config(function($routeProvider) {                                       
    $routeProvider
        .when("/", {
        templateUrl : "tutorials.html"
        })
        .when("/html", {
        templateUrl : "html.html"
        })
        .when("/css", {
        templateUrl : "css.html"
        })
        .when("/php", {
        templateUrl : "php.html"
        });
    });

Code Explanation for Routing in AngularJS:

Code Explanation for AngularJS Routing

  1. To use AngularJS Routing we should include "angular-route.js" JavaScript source file from googleapis.com.
  2. The set of navigation links are specified in <a> tag for different templates with different external files.
  3. The ng-view directory is used to show the rendered template in the Current Application.
  4. The angular.module used to create an Application module for “myApp” module.
  5. The “ngRoute” is used to specify the dependency of routing in the application module.
  6. The config is used to configure the application with $routeprovider.
  7. The .when used to specify when the templateUrl routed to the view.
  8. The set of templateUrl for $routeProvider for the navigation links.

Sample Output for Animation in AngularJS:

    Sample Output1 for AngularJS Routing

  1. On page load by default “tutorials.html” page is routed.
  2. Sample Output2 for AngularJS Routing

  3. When user click the HTML link then “html.html” page is routed. And the URL changed to #/html.
  4. Sample Output3 for AngularJS Routing

  5. When user click the CSS link then “css.html” page is routed. And the URL changed to #/css.
  6. Sample Output4 for AngularJS Routing

  7. When user click the PHP link then “php.html” page is routed. And the URL changed to #/php.


Related Searches to angularjs routing