理解AngularJS ngRoute

來源:互聯網
上載者:User

標籤:http   os   java   使用   io   strong   ar   art   html   

一般來說,我們認為AngularJS是一套前端的MVC架構。那麼,為了實現視圖的中轉,肯定會涉及到路由的概念。這裡我們就來說說有關AngularJS的路由——ngRoute。

序

個人瞭解到AngularJS,是由於在尋找可以動態利用Ajax從伺服器端取得部分頁面這一功能而找到EmberJS,而後又根據EmberJS找到的AngularJS。而在AngularJS中,實現這一功能的,就是ngRoute。

所以,個人與AngularJS的結緣,是由於ngRoute一點也不為過。

理解

AngularJS的基礎是其模組機制。在模組的基礎上,我們可以利用JavaScript的一些特性來做我們想做的任何的事情。

ngRoute是一個AngularJS的模組。其不是在AngularJS的核心庫當中。

在使用ngRoute的時候,實際上,我們是在應用的主模組中引入ngRoute模組並添加$routeProvider服務到主模組的config方法中來達到我們的目標。這與其他一些庫的使用方式是類似的,如ng-grid。

在ngRoute中,主要有$route、$routeProvider、$routeParams三個服務項目。

$routeProvider用於在主應用主模組的配置方法中。$route與$routeParams一般常見於控制器中。

安裝
  1. 使用Google CDN

1
http://ajax.googleapis.com/ajax/libs/angularjs/X.Y.Z/angular-route.js

  1. 使用Bower

由於國內網路環境的和諧,個人強烈建議使用此種方式!

1
bower install [email protected]

  1. 使用官方CDN

1
http://code.angularjs.org/X.Y.Z/angular-route.js

使用

ngRoute並沒有在AngularJS的核心包裡面,所以在使用的時候,需要引入一個獨立的庫。

  1. 在HTML中引入ngRoute

1234
<!-- index.html --><script src="angular.js" /><script src="angular-route.js" />

  1. 在應用主模組中,引入ngRoute

123
 myApp = angular.module(, []);

  1. 在模組的config中綁定URL路徑與控制器

12345678910111213141516
 configFn =     $routeProvider.      when(, {        templateUrl: ,        controller:       }).      when(, {        templateUrl: ,        controller:       }).      otherwise({        redirectTo:       });};myApp.config(configFn);

$routeProvider

$routeProvider是一個用於配置路由的內建服務。由於它是一個服務,根據service的使用建議,我們主要將其當做為工具來使用,所以我們一般直接使用$routeProvider.XXX來調用它的成員方法來實現一定的功能,而不是執行個體化一個$routeProvider的執行個體。它主要有以下兩個成員函數:

  1. otherwise(params):設定映射資訊到$route.current,一般用於指定沒有標明的路由如何處理。
  2. when(path, route):向$route服務添加新的路由。path是指定的URL路徑,route標明路由的處理。

$routeProvider.when(path, route)中的路由處理參數對象,常用的有如下的屬性:

  • controller:用於指向用於路由處理的控制器。
  • template:用於指向用於路由的視圖模板。
  • redirectTo:重新導向。(使用$location)

不常用屬性:

  • controllerAs:控制器別名,一旦設定,在視圖中的scope就得使用這個別名來調用控制器。
  • templateUrl
  • resolve
$route

$route用於將控制器與視圖相連。它觀察$location.url(),並且嘗試映射路徑到一個已經存在的路由定義中。它依賴$location、$routeParams。它主要有如下成員函數:

  • reload() 用於重新載入當前的路由,哪怕$location沒有改變。調用後,ngView將建立新的Scope等。

它主要有如下事件:

  • $routeChangeStart:路由改變之前觸發;
  • $routeChangeSuccess:路由改變之後觸發;
  • $routeChangeError:路由改變出錯觸發;
$routeParams

$routeParams允許你檢索當前路由的參數。主要使用$location的search()與path()方法。

參考
  • http://docs.angularjs.org/tutorial/step_07
  • http://docs.angularjs.org/api/ngRoute
  • http://docs.angularjs.org/api/ngRoute.$route
  • http://docs.angularjs.org/api/ngRoute.$routeParams
  • http://docs.angularjs.org/api/ngRoute.$routeProvider

理解AngularJS ngRoute

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.