常用事件:
$ routeChangeStart:這個事件會在路由跳轉前觸發;$ routeChangeSuccess:這個事件在路由跳轉成功后觸發;$ routeChangeError:這個事件在路由跳轉失敗后觸發;1、引入對應的js文件。
angular.js 和 angular-route.js,angular.js必須放在前邊。因為 angular-route.js 會使用到 window.angular 這個參數,而這個參數只有在加載完 angular 才會出現。2、HTML
<!DOCTYPE html><html><head> <meta charset="utf-8"> <title></title> <script type="text/javascript" src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script> <script type="text/Javascript" src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular-route.min.js"></script> <script type="text/javascript" src="index.js"></script></head><body ng-app="Demo"> <ul> <li><a href="#/">首頁</a></li> <li><a href="#/computers">電腦</a></li> <li><a href="#/printers">打印機</a></li> <li><a href="#/blabla">其他</a></li> </ul> <div ng-view></div></body></html>3、JS/Route
angular.module('Demo', ['ngRoute']) .config(['$routeProvider', function($routeProvider) { $routeProvider .when('/', { template: '這是首頁頁面' }) .when('/computers', { template: '這是電腦分類頁面' }) .when('/printers', { template: '這是打印機頁面' }) .otherwise({ redirectTo: '/' }); }]);新聞熱點
疑難解答