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
About Angular ngRoute
From AngularJS 1.2.0 routing has changed. It is nowrequired that you explicitly import ngRoute in your applications.
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 inngRouteModule) is used to configure the routes. We use the module's config() to configure the $routeProvider. Theconfig()takes a function that takes the$routeProvideras 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 RequiredWe can create routing using the following mentioned code in our web application: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: '/' }); }]);This process is OK if your web applicable contains a limited number of pages. But it becomes difficult when we have a large number of HTML files and we need to populate the navigation URL details depending on the user-defined rule, perhaps just like menus in a web application. In those case, we need to to configure the routeconfig dynamically by reading the data from an outer soure file (in other Words from a database or JSON file) when we can change or rearrange the data as required.For doing this, we declare a variable and assign that variable with routeProvider in app.Config. Then we read the data from the soure and generate a route string and assign that into the routeProvider variable in the app.Run. Becuase, in angular JS, the execution sequence of the app and their services are as follows:
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>
新闻热点
疑难解答