AngularJS Routing Example - ngRoute, $routeProvider

Today we will look into AngularJS Routing example using ngRoute module and $routeProvider. Earlier we looked into AngularJS Modules and AngularJS Controllers.

Routing in AngularJS

Routing in AngularJS is one of the core feature. In this AngularJS routing example, we will build a small single page application with multiple views to show you how routing in AngularJS works.


AngularJS ngRoute module provides routing, deep linking services and directives for angular applications. We have to download angular-route.js script that contains the ngRoute module from AngularJS official website to use the routing feature.

You can also use the CDN in your application to include this file. In this tutorial, We are going to use the Google CDN.

If you are bundling this file into your application, then you can add it to your page with below code.

If you want to include it from Google CDN, then use below code.

Then load the ngRoute module in your AngularJS application by adding it as a dependent module as shown below.


ngView directive is used to display the HTML templates or views in the specified routes. Every time the current route changes, the included view changes with it according to the configuration of the $route service.


$routeProvider is used to configure the routes. We use the ngRoute config() to configure the $routeProvider. The config() takes a function which takes the $routeProvider as parameter and the routing configuration goes inside the function.

$routeProvider has a simple API, accepting either the when() or otherwise() method.

AngularJS Routing Syntax

The following syntax is used to configure the routes in AngularJS.

when() method takes a pathand a route as parameters.

path is a part of the URL after the # symbol.

route contains two properties – templateUrl and controller.

templateUrl property defines which HTML template AngularJS should load and display inside the div with the ngView directive.

controller property defines which controllers should be used with the HTML template.

When the application is loaded, path is matched against the part of the URL after the # symbol. If no route paths matches the given URL the browser will be redirected to the path specified in the otherwise() function.

AngularJS Routing Example

Now let’s go through a simple example to understand the AngularJS rounting. At first, we will define a module, some routes, create controllers and create multiple views. Finally, we will create the shell page of our application to hold the multiple views.

  1. Create a module named mainApp and load ngRoute as a dependent module.
  2. Configure the routes using $routeProvider.
  3. We use two paths in the example, /home and /viewStudents.
  4. We use only a single controller in this example, StudentController
  5. StudentController is initialized with an array of students and a message. We will be showing the message in the home page and the students list in viewStudents page.
  6. Save this file as main.js


For example, if the URL is like, The URL part after the # matches /home, it will load home.html page and if it matches /viewStudents then it will load viewStudents.html in to the shell page. If nothing matches then it will go in otherwise condition and page will be redirected to home.html.

Now we can create our views and save as home.html and viewStudents.html files.


This is the default page of our application. In this view, we just print out the message, which we have already initialized in the StudentController. You can also see a link to the viewStudents page.


In the above view, you can see a list of students with a search option.

Finally, follow below steps to complete our AngularJS routing example application.

  • ng-app auto-bootstraps our application mainApp
  • ngView directive is the placeholder of the views – home.html and viewStudents.html
  • Include angular.min.js and angular-route.min.js
  • Include main.js which we have created in the earlier steps.
  • Save the file as index.html


That’s all for our AngularJS Routing example. Our application is ready to run.

Run your application

  • Save all the files in the same directory.
  • open index.html from your browser
  • Try our online demo.

AngularJS Routing Example Online Demo

By admin

Leave a Reply

%d bloggers like this: