The need for Routing
Normally, when we create a multiple page application or website, we always need to navigate among various pages or views. With ASP.Net, we can do that using the window.redirect method. But in angularjs, we can do this using a Single Page Application. Single Page Applications are becoming very popular today due to phone/tablet apps. Angular JS helps to easily create applications like that.
Benefits of a Single Page Application
- Single Page Application.
- No Page refresh on page change.
- Multiple page data on the same page.
About Angular ngRoute
From AngularJS 1.2.0 routing has changed. It is now required that you explicitly import ngRoute in your applications.
- <script src=>
- <script src="angular-route.js">
- angular.module('app', []);
ngView: The ngView directive displays the HTML templates or views in the specified routes. Every time the current route changes, the included view changes with it depending on the configuration of the $route service.
$routeProvider: The $routeProvider (provider in ngRoute Module) is used to configure the routes. We use the module's config() to configure the $routeProvider. The config() takes a function that 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. $routeProvider.when() is used to match a URL pattern to a view whereas $routeProvider.otherwise() is used to render a view when there is no match to a URL pattern.
Why Dynamic Routing is Required- var module = angular.module("MyApp", ['ngRoute']);
- module.config(['$routeProvider',
- function($routeProvider) {
- $routeProvider.
- when('/Home', {
- templateUrl: 'Home.html',
- controller: 'HomeController'
- }).
- when('/1st', {
- templateUrl: 'First.html',
- controller: 'FirstController'
- }).
- otherwise({
- redirectTo: '/'
- });
- }]);
- app.config
- app.run
- other services and directive compilation start
- var $routeProviderReference;
- MyApp.config(function ($routeProvider) {
- $routeProviderReference = $routeProvider;
- });
- MyApp.run(['$route', '$http', '$rootScope',
- function ($route, $http, $rootScope) {
- $http.get("../Script/User/routedata.json").success(function (data) {
- var iLoop = 0, currentRoute;
- for (iLoop = 0; iLoop < data.records.length; iLoop++) {
- currentRoute = data.records[iLoop];
- var routeName = "/" + currentRoute.KeyName;
- $routeProviderReference.when(routeName, {
- templateUrl: currentRoute.PageUrls
- });
- }
- $route.reload();
- });
- }]);
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title>Home Page</title>
- <script src="../Script/refScript/angular1.3.8.js"></script>
- <script src="../Script/refScript/angular-route.js"></script>
- <script src="../Script/User/MyApp.js"></script>
- <script src="../Script/User/DynaRoute.js"></script>
- <script src="../Script/User/HomeController.js"></script>
- <script src="../Script/User/FirstController.js"></script>
- <script src="../Script/User/SecondController.js"></script>
- <script src="../Script/User/ThirdController.js"></script>
- </head>
- <body ng-app="MyApp" ng-controller="HomeController">
- <table style="width:100%;">
- <tr>
- <td><a ng-click="fnGoToPage('home');">Home</a></td>
- <td><a ng-click="fnGoToPage('1st');">First Page</a></td>
- <td><a ng-click="fnGoToPage('2nd');">Second Page</a></td>
- <td><a ng-click="fnGoToPage('3rd');">Third Page</a></td>
- </tr>
- </table>
- <div class="pgHolder" ng-view>
- </div>
- </body>
- </html>
- var MyApp = angular.module('MyApp', ['ngRoute']);
- {
- "records": [
- {
- "KeyName": "home",
- "PageUrls": "../Html/Home.html",
- "Controller": "HomeController"
- },
- {
- "KeyName": "1st",
- "PageUrls": "../Html/FirstPage.html",
- "Controller": "FirstController"
- },
- {
- "KeyName": "2nd",
- "PageUrls": "../Html/SecondPage.html",
- "Controller": "SecondController"
- },
- {
- "KeyName": "3rd",
- "PageUrls": "../Html/ThirdPage.html",
- "Controller": "ThirdController"
- }
- ]
- }
- MyApp.controller("HomeController", ['$scope', '$http', '$location',
- function ($scope, $http, $location) {
- $scope.fnGoToPage = function (args) {
- $location.path('/' + args);
- }
- }
- ]);
- <div ng-controller="FirstController">
- {{Message1}}
- </div>
- MyApp.controller("FirstController", ['$scope', '$http',
- function ($scope, $http) {
- $scope.Message1 = "First page";
- }
- ]);

Sr KarthigaPosted Apr 19, 2016, 11:04 PM
Nice explanation
Sandeep SinghPosted Mar 6, 2016, 11:41 PM
Debasis Saha I found some error in your article. You have written "With ASP.Net, we can do that using the window.redirect method" but there is no such kind of method in ASP.Net. I have used Response.Redirect and Server.Transfer but I never heard about window.redirect.
stefano pietrellaPosted Jul 2, 2015, 9:09 AM
Good one
rohan tangadePosted Jun 23, 2015, 1:34 AM
Good One
Jitendra KumarPosted Apr 21, 2015, 5:37 AM
Good Article..
Debasis SahaPosted Apr 14, 2015, 1:36 PM
Thank you friends....
Karthik Muthu KaruppanPosted Apr 13, 2015, 1:37 PM
Good one
NitinPosted Apr 13, 2015, 2:41 AM
good show
Sibeesh VenuPosted Apr 13, 2015, 2:00 AM
Good one :)
Vithal WadjePosted Apr 12, 2015, 1:03 PM
nice to learn thanks for sharing
Tom MohanPosted Apr 12, 2015, 3:59 AM
Nice one.
Santhakumar MunuswamyPosted Apr 11, 2015, 12:48 PM
Good