<!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>無標題文檔</title> <script src="angular.min.js"></script> <script src="http://cdn.bootcss.com/angular.js/1.2.9/angular-route.min.js"></script> <script> var m1 = angular.module('myApp',['ngRoute']);//引入插件angular-route.min.js m1.config(['$routeProvider',function($routeProvider){//通過供應商初始化配置 $routeProvider .when('/aaa/:int',{//int對應123,456 template : '<p>首頁的內容</p>{{name}}', //模版加入到ng-view controller : 'Aaa'//改變name的值 }) .when('/bbb',{ template : '<p>學員的內容</p>{{name}}', controller : 'Bbb' }) .when('/ccc',{ template : '<p>課程的內容</p>{{name}}', controller : 'Ccc' }) .otherwise({//初始調用的時候走這里 redirectTo : '/aaa' }); }]); m1.run(['$rootScope',function($rootScope){ $rootScope.$on('$routeChangeStart',function(event,current,pre){//routeChangeStart是ngRoute切換之前執行的, console.log(event); console.log(current); console.log(pre); }); }]); m1.controller('Aaa',['$scope','$location','$routeParams',function($scope,$location,$routeParams){ $scope.name = 'hello'; $scope.$location = $location; console.log( $routeParams ); }]); m1.controller('Bbb',['$scope',function($scope){ $scope.name = 'hi'; }]); m1.controller('Ccc',['$scope',function($scope){ $scope.name = '你好'; }]); </script> </head> <body> <div ng-controller="Aaa"> <a href="" ng-click="$location.path('aaa/123')">首頁</a> //$location.path切換路徑,$location對應controller里面的$scope.$location <a href="" ng-click="$location.path('bbb')">學員</a> <a href="" ng-click="$location.path('aaa/456')">課程</a> <div ng-view></div>//切換布局的主題 </div> </body> </html>