AngularJS routing allows us to access different content through different URLs.
AngularJS can realize single page web application (SPA) for multi-views.
Usually our URL is http://runoob.com/first/page , but in a single page web application AngularJS is implemented through #+ tags, for example:
http://runoob.com/#/first
http://runoob.com/#/second
http://runoob.com/#/third
Let’s first look at the configuration object properties method of $routeProvider:
Routing setting object resolution:
$routeProvider.when(url(routing name), { template: string(template prompt string), templateUrl: string(template path URL), controller: string, function or array (create the controller in the current template and generate a new $scope scope), controllerAs: string(controller alias), redirectTo: string, function(redirect address), resolve: object<key, function> (module that the current route depends on)});The general steps to implement routing:
Step 1: Import the ngRoute module
<script type="text/javascript" src="js/angular-route.min.js"></script>
Step 2: Use ngRoute in the application module
angular.module("routeApp", ["ngRoute"])
Step 3: Use the ng-view command
<div ng-view ng-controller='defaultCtrl'></div>
Step 4: Configure $routeProvider routing rules
....config(['$routeProvider', function ($routeProvider){ $routeProvider .when('/home', { templateUrl : 'home.tpl.html', controller : 'HomeCtrl', }) .when('/computer', { templateUrl : 'computer.html', }) .when('/phone', { templateUrl : 'phone.html', }) .when('/other', { templateUrl : 'other.tpl.html', controller : 'OtherCtrl', })}])...Step 5: Use routing through hyperlinks
<ul> <li><a href="#/home">Home</a></li> <li><a href="#/computer">Computer</a></li> <li><a href="#/phone">Mobile</a></li> <li><a href="#/other">Other</a></li></ul>
Complete case:
1 route.html page
<html> <head> <meta charset="utf-8"> <title>AngularJS routing instance</title> <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css"> </head> <body ng-app='routeApp'> <ul> <li><a href="#/home">Home</a></li> <li><a href="#/computer">Computer</a></li> <li><a href="#/phone">Mobile</a></li> <li><a href="#/other">Other</a></li> </ul> <div ng-view ng-controller='defaultCtrl'></div> <script type="text/javascript" src="js/jquery.min.js"></script> <script type="text/javascript" src="js/bootstrap.min.js"></script> <script type="text/javascript" src="js/angular.min.js"></script> <script type="text/javascript" src="js/angular-route.min.js"></script> <script type="text/ng-template" id="home.tpl.html"> <h1>{{data}}</h1> </script> <script type="text/ng-template" id="other.tpl.html"> <h1>{{data}}</h1> </script> <script type="text/javascript"> angular.module("routeApp", ["ngRoute"]) .config(['$routeProvider', function ($routeProvider){ $routeProvider .when('/home', { templateUrl : 'home.tpl.html', controller : 'HomeCtrl', }) .when('/computer', { templateUrl : 'computer.html', }) .when('/phone', { templateUrl : 'phone.html', }) .when('/other', { templateUrl : 'other.tpl.html', controller : 'OtherCtrl', }) }]) .controller('defaultCtrl', function ($scope) { $scope.computers = [ { id: 0, name: "Acr", category: "Test", price: 1.25 }, { id: 1, name: "Lenovo", category: "Test", price: 2.45 }, { id: 2, name: "Apple", category: "Test", price: 4.25 } ]; $scope.phones = [ { id: 0, name: "Samsung", category: "Test", price: 1.25 }, { id: 1, name: "Glory", category: "Test", price: 2.45 }, { id: 2, name: "Meizu", category: "Test", price: 4.25 } ]; }) .controller("HomeCtrl", function ($scope, $route) { $scope.$route = $route; $scope.data = "Home Home"; }) .controller("OtherCtrl", function ($scope, $route) { $scope.$route = $route; $scope.data = "Other Other"; }) </script> </body> </html>2.computer.html page
<div> <table> <thead> <tr> <th>Name</th> <th>Category</th> <th>Price</th> <th>{{data}}</th> </tr> </thead> <tbody> <ttr ng-repeat="item in computers"> <td>{{item.name}}</td> <td>{{item.category}}</td> <td>{{item.price | currency}}</td> <td> <button ng-click="deleteProduct(item)">Delete</button> <a href="/edit/{{item.id}}" ng-click="editOrCreateProduct(item)">Edit</a> <button ng-click="incrementPrice(item)">+</button> </td> </tr> </tbody> </table> <div> <button ng-click="editOrCreateProduct()">Add</a> </div></div>3.phone.html page
<div> <table> <thead> <tr> <th>Name</th> <th>Category</th> <th>Price</th> <th>{{data}}</th> </tr> </thead> <tbody> <ttr ng-repeat="item in phones"> <td>{{item.name}}</td> <td>{{item.category}}</td> <td>{{item.price | currency}}</td> <td> <button ng-click="deleteProduct(item)">Delete</button> <a href="/edit/{{item.id}}" ng-click="editOrCreateProduct(item)">Edit</a> <button ng-click="incrementPrice(item)">+</button> </td> </tr> </tbody> </table> <div> <button ng-click="editOrCreateProduct()">Add</a> </div></div>Click Home Page
Click "Computer"
Click "Mobile"
Click Other
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.