Dynamic Routing Using Angular JS
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 Requiredvar 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();
});
}]);
Now, we create a HTML file named HomePage.Html and write the following code.
<!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>

浙公网安备 33010602011771号