Please enable Javascript to correctly display the contents on Dot Net Tricks!

Understanding AngularJS Routing

  Author : Shailendra Chauhan
Updated On : 16 Sep 2014
Total Views : 15,935   
 

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:

http://yourdomain.com/index.html#users
http://yourdomain.com/index.html#orders
http://yourdomain.com/index.html#books
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.js JavaScript 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:

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

<script>
 appmodule.config(['$routeProvider',
 function($routeProvider) {
 $routeProvider.
 when('/route1', {
 templateUrl: 'template-1.html',
 controller: 'RouteController1'
 }).
 when('/route2:param', {
 templateUrl: 'template-2.html',
 controller: 'RouteController2'
 }).
 otherwise({ // if no route paths matches
 redirectTo: '/' 
 });
 }]);
</script>

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

<a href="#/route1">Route 1</a><br/>
<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.

<div ng-view></div>

An Example of Routing

index.html

<!DOCTYPE html>
<html>
<head>
 <title>AngularJS Routing</title>
 <script src="lib/angular.js"></script>
 <script src="lib/angular-route.js"></script>

 <script>
 var app = angular.module("app", ['ngRoute']);

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

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

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

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

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

 $scope.message = "Welcome to Contact Page!";
 });
 
 app.config(['$routeProvider', function ($routeProvider) {
 $routeProvider.when("/", {
 templateUrl: "templates/pages/home.html",
 controller: "homeController"
 }).when("/about", {
 templateUrl: "templates/pages/about.html",
 controller: "aboutController"
 }).when("/contact", {
 templateUrl: "templates/pages/contact.html",
 controller: "contactController"
 }).otherwise({
 redirectTo: 'index.html'
 });
 }]);

 </script>
</head>
<body ng-app="app">
 <div>
 <a href="#">Home</a> |
 <a href="#contact">Contact</a> |
 <a href="#about">About</a>
 </div>
 <div ng-view></div>
</body>
</html>

home.html

<h1>Home Page</h1>
<h2>{{message}}</h2>

about.html

<h1>About Page</h1>
<h2>{{message}}</h2>

contact.html

<h1>Contact Page</h1>
<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.


YOU MIGHT LIKE
Free Interview Books
 
COMMENTS (0)
13 FEB
ASP.NET Core Development (online)

07:00 AM-09:00 AM IST( MON, WED, FRI)

Know More
25 JAN
Angular2 and Angular1 Development (online)

09:00 PM-10:30 PM IST(MON-FRI)

Know More
24 JAN
ASP.NET MVC with Angular2 Development (online)

07:00 AM-09:00 AM IST(Tuesday & Thursday)

Know More
20 JAN
NODEJS & MEAN Stack 2 Development (online)

09:00 PM-11:00 PM IST( MON, WED, FRI)

Know More
15 JAN
PPC Marketing (offline)

04:00 PM-05:300 PM

14 JAN
Xamarin Forms : Build Cross-platform Apps (offline)

05:00 PM-06:30 PM IST

10 JAN
ASP.NET MVC with AngularJS Development (online)

09:00 PM-11:00 PM IST on (TUE, THRU,SAT)

3 JAN
ASP.NET MVC with AngularJS Development (online)

07:00 AM-09:00 AM IST

31 DEC
ASP.NET MVC with AngularJS Development (offline)

09:30 AM-11:00 AM

31 DEC
Angular2 and Angular1 Development (offline)

11:00 AM-12:30 AM

26 NOV
ASP.NET MVC with AngularJS Development (offline)

03:00 PM-04:30 PM

5 NOV
ASP.NET MVC with AngularJS Development (offline)

08:00 AM-09:30 AM

4 SEP
MEAN Stack 2 Development (offline)

11:00 AM-12:30 PM.

BROWSE BY CATEGORY
 
RECENT ARTICLES
SUBSCRIBE TO LATEST NEWS
 
LIKE US ON FACEBOOK
 

Professional Speaks

+