標籤:rect 模板檔案 web pps str view 使用 服務 img
1.AngularJS路由允許我們通過不同的URL訪問不同的內容;通過AngularJS可以實現多視圖的單頁WEB訪問(SPA)
2.通常我們的URL形式為http://runoob.com/first/page,但在單頁Web應用中AngularJS通過#+標記實現,例如:
http://runoob.com/#/first
http://runoob.com/#/second
http://runoob.com/#/third
3.當我們點擊以上的任意的一個連結時,向服務端請的地址都是一樣的(http://runoob.com/)。因為#號之後的內容在向服務端請求時會被瀏覽器忽略掉。所以我們就需要在用戶端實現#號後面內容的功能實現。AngularJS路由就通過#+標記協助我們區分不同的邏輯頁面並將不同的頁面綁定到對應的控制器上。
4.簡單一實例:
<html>
<head>
<meta charset="utf-8">
<title>AngularJS路由執行個體<title>
</head>
<body ng-app=‘routingDemoApp‘>
<h2>AnuglarJS路由應用</h2>
<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> //使用ngView指令,該<div>內的HTML內容會根據路由的變化而變化
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
<script src="http://apps.bdimg.com/libs/angular-route/1.3.13/angular-route.js"></script> //載入了實現路由的js檔案:angular-route.js
<script>
angular.module(‘routingDemoApp‘,[‘ngRoute‘]) //包含了ngRoute模組作為主應用模組的相依模組
.config([‘$routeProvider‘,function($routeProvider{ //AngularJS模組的config函數用於配置路由規則,通過使用configAPI,我們請求把$routeProvider注入到我們的配置函數且使用$routeProviderAPI定義我們的路由規則
$routeProvider; //配置$routeProvider,AngularJS的$routeProvider用了定義路由規則
.when(‘/‘,{template:‘這是首頁頁面‘}) //$routeProvider為我們提供了when(path,object)&otherwise(object)函數按順序定義所有路由,函數包含兩個參數:第一個參數是URL或者URL正則規則;第二個參數是路由設定物件
.when(‘/computers‘,{template:‘這是電腦分類頁面‘})
.when(‘/printers‘,{template:‘這是印表機頁面‘})
})]);
</script>
</body>
</html>
5.路由設定對象:AngularJS路由也可以通過不同的模板來實現,$routeProvider.when函數的第一個參數是URL或者URL正則規則,第二個參數為路由設定物件;文法規則如下:
$routeProvider.when(url,{
template:string, //如果我們只需要在ng-view中插入簡單的HTML內容,則使用該參數.when(‘/computers‘,{template:‘這是電腦分類頁面‘})
templateUrl:string, //如果我們只需要在ng-view中插入HTML模板檔案,則使用該參數:$routeProvider.when(‘/computers‘,{templateUrl:‘views/computers.html‘,});從伺服器端擷取view/computers.html檔案插入到ng-view中
controller:string,function或array, //function,string,或數群組類型,在當前模板上執行的controller函數,產生新的scope
controllerAs:string, //string,為controller指定別名
redirectTo:string,function, //重新導向的地址
resolve:object<key,function> //指定當前controller所依賴的其他模組
});
Angular JS 學習之路由