AngularJS的路由在实际应用中更多是由另外封装好的angular-ui-router.js实现的!
为什么不用Ajax而要用前端路由?
示例:
<!doctype html> <html ng-app="routerApp"> <head> <meta charset="utf-8"> <link rel="stylesheet" href="css/bootstrap-3.0.0/css/bootstrap.css"> <link rel="stylesheet" href="css/index.css"> <script src="js/angular-1.3.0.js"></script> <script src="js/angular-animate.js"></script> <script src="js/angular-ui-router.js"></script> <script src="UIRoute3.js"></script> </head> <body> <div ui-view></div> </body> </html>
var routerApp = angular.module('routerApp', ['ui.router']); routerApp.config(function($stateProvider, $urlRouterProvider) { $urlRouterProvider.otherwise('/index'); $stateProvider .state('index', { url: '/index', views: { '': { templateUrl: 'tpls3/index.html' }, 'topbar@index': { templateUrl: 'tpls3/topbar.html' }, 'main@index': { templateUrl: 'tpls3/home.html' } } }) .state('index.usermng', { url: '/usermng', views: { 'main@index': { templateUrl: 'tpls3/usermng.html', controller: function($scope, $state) { $scope.addUserType = function() { $state.go("index.usermng.addusertype"); } } } } }) .state('index.usermng.highendusers', { url: '/highendusers', templateUrl: 'tpls3/highendusers.html' }) .state('index.usermng.normalusers', { url: '/normalusers', templateUrl: 'tpls3/normalusers.html' }) .state('index.usermng.lowusers', { url: '/lowusers', templateUrl: 'tpls3/lowusers.html' }) .state('index.usermng.addusertype', { url: '/addusertype', templateUrl: 'tpls3/addusertypeform.html', controller: function($scope, $state) { $scope.backToPrevious = function() { window.history.back(); } } }) .state('index.permission', { url: '/permission', views: { 'main@index': { template: '这里是权限管理' } } }) .state('index.report', { url: '/report', views: { 'main@index': { template: '这里是报表管理' } } }) .state('index.settings', { url: '/settings', views: { 'main@index': { template: '这里是系统设置' } } }) });
以上部分内容来自慕课网大漠穷秋老师的视频截图: