ng的路由機制是靠ngRoute提供的,通過hash和history兩種方式實現了路由,可以檢測瀏覽器是否支持history來靈活調用相應的方式。ng的路由(ngRoute)是一個單獨的模塊,包含以下內容:
服務$routeProvider用來定義一個路由表,即地址欄與視圖模板的映射.
服務$routeParams保存了地址欄中的參數,例如{id : 1, name : 'tom'}.
服務$route完成路由匹配,並且提供路由相關的屬性訪問及事件,如訪問當前路由對應的controller.
指令ngView用來在主視圖中指定加載子視圖的區域
以上內容再加上$location服務,我們就可以實現一個單頁面應用了。
第一步:引入文件和依賴
ngRoute模塊包含在一個單獨的文件中,所以第一步需要在頁面上引入這個文件,如下:
<script src="http://code.angularjs.org/1.2.8/angular.min.js" rel="nofollow"/>
<script src="http://code.angularjs.org/1.2.8/angular-route.min.js" rel="nofollow"/>
我們還需在模塊聲明中注入對ngRoute的依賴,如下:
var app = angular.module('MyApp', ['ngRoute']);
第二步:定義路由表
$routeProvider提供了定義路由表的服務,它有兩個核心方法,when(path,route)和otherwise(params),先看一下核心中的核心when(path,route)方法。
when(path,route)方法接收兩個參數,path是一個string類型,表示該條路由規則所匹配的路徑,它將與地址欄的內容($location.path)值進行匹配。如果需要匹配參數,可以在path中使用冒號加名稱的方式,如:path爲/show/:name,如果地址欄是/show/tom,那麼參數name和所對應的值tom便會被保存在$routeParams中,像這樣:{name : tom}。我們也可以用*進行模糊匹配,如:/show*/:name將匹配/showInfo/tom。
route參數是一個object,用來指定當path匹配後所需的一系列配置項,包括以下內容:
controller //function或string類型。在當前模板上執行的controller函數,生成新的scope;
controllerAs //string類型,爲controller指定別名;
template //string或function類型,視圖z所用的模板,這部分內容將被ngView引用;
templateUrl //string或function類型,當視圖模板爲單獨的html文件或是使用了<script type="text/ng-template">定義模板時使用;
resolve //指定當前controller所依賴的其他模塊;
redirectTo //重定向的地址。
最簡單情況,我們定義一個html文件爲模板,並初始化一個指定的controller:
function emailRouteConfig($routeProvider){
$routeProvider.
when('/show', {
controller: ShowController,
templateUrl: 'show.html'
}).
when('/put/:name',{
controller: PutController,
templateUrl: 'put.html'
});
};
otherwise(params)方法對應路徑匹配不到時的情況,這時候我們可以配置一個redirectTo參數,讓它重定向到404頁面或者是首頁。
第三步:在主視圖模板中指定加載子視圖的位置
我們的單頁面程序都是局部刷新的,那這個“局部”是哪裏呢,這就輪到ngView出馬了,只需在模板中簡單的使用此指令,在哪裏用,哪裏就是“局部”。例如:
<div ng-view></div> 或:<ng-view></ng-view>
代碼簡單如下:
var app = angular.module("personApp", ['ngRoute']);
app.config(['$routeProvider',function ($routeProvider) {
$routeProvider
.when('/:tid', {
templateUrl: 'person-route.html',
controllerAS: 'personInfoCon'
})
.otherwise({
templateUrl: 'person-route.html',
controllerAs: 'personInfoCon'
});
}]);
app.controller("personInfoCon", function($scope, $routeParams, PersonService, AboutTopicService, CommentService){
$scope.tid = "127";
$scope.page = "1";
$scope.pagesArray = [];
if($routeParams.tid) {
$scope.tid = $routeParams.tid;
}
......
}
<div class="aboutperson-block" ng-repeat="person in persons">
<a href="#/{{person.teacherId}}">
<img class="aboutperson-block-img"ng-src="{{person.url}}">
<label class="aboutperson-block-name">{{person.name}}</label>
<label class="aboutperson-block-topic">{{person.topic}}</label>
<label class="aboutperson-block-simpleinfo">{{person.simpleInfo}} CEO</label>
<label class="aboutperson-block-likeno"><img src="http://image.1yingli.cn/img/kelly.png"><span>{{person.likenumber}}人想見</span></label>
</a>
</div>
或者:
demoApp.config(['$routeProvider',function($routeProvider) {
$routeProvider.when('/list', {
templateUrl: 'route/list.html',
controller: 'routeListController'
}).when('/list/:id', {
templateUrl: 'route/detail.html',
controller: 'routeDetailController'
}).otherwise({
redirectTo: '/list'
});
}]);
controller注意事項:
controller 和controllerAs的區別,頁面加載獲取數據會加載兩次。
ng-controller的初始化位置,頁面初始化開始加載ng-controller,所有要避免無用的http請求。
https://github.com/johnpapa/angular-styleguide/blob/master/i18n/zh-CN.md#
http://www.lovelucy.info/angularjs-best-practices.html
https://code.angularjs.org/1.4.7/docs/api