最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

angular學(xué)習(xí)之ngRoute路由機(jī)制

 更新時(shí)間:2017年04月12日 16:16:06   作者:吳冬冬  
這篇文章主要介紹了angular學(xué)習(xí)之ngRoute路由機(jī)制,ngRoute是一個(gè)Module,提供路由和深層鏈接所需的服務(wù)和指令。有需要的可以了解一下。

ngRoute簡(jiǎn)介

路由是AngularJS很重要的一環(huán),它可以把你項(xiàng)目的頁(yè)面串聯(lián)起來,構(gòu)成一個(gè)項(xiàng)目,常用的路由有ngRoute和ui-route,我這里先講ngRoute。ngRoute是一個(gè)Module,提供路由和深層鏈接所需的服務(wù)和指令。

注意一點(diǎn),和之前的文章不一樣,使用ngRoute之前你需要引入另外一個(gè)js文件angular-route.js:

<script src="script/angular.min.js"></script>
<script src="script/angular-route.min.js"></script>

ngRoute包含內(nèi)容如下:

名稱 類型 作用
ngView Directive 路由的不同模板其實(shí)都是插入這個(gè)元素里
$routeProvider Provider 路由配置
$route Service 各個(gè)路由的url,view,controller
$routeParams Service 路由參數(shù)

使用ngRoute的基本流程如下:

  1. 在需要路由的地方使用ngView來定義視圖模板。
  2. 在module中注入ngRoute模塊
  3. 在config中用$routeProvider來對(duì)路由進(jìn)行配置templateUrl,controller,resolve。
  4. 在每個(gè)controller中寫業(yè)務(wù)邏輯
  5. 在controller中可以通過注入$routeParams來獲得url上的參數(shù)

可以看下下面這個(gè)例子

color.html

<!DOCTYPE html>
<html>
<head>
  <meta charset="uft-8"/>
  <title></title>
</head>
<script src="script/angular.min.js"></script>
<script src="script/angular-route.min.js"></script>
<body ng-app="color">
<p><a href="#/" rel="external nofollow" rel="external nofollow" >Main</a></p>

<a href="#red" rel="external nofollow" rel="external nofollow" >Red</a>
<a href="#green" rel="external nofollow" rel="external nofollow" >Green</a>

<div ng-view></div>

</body>

<script>
  var app = angular.module("color", ["ngRoute"]);

  app.config(function ($routeProvider) {
    $routeProvider
        .when("/", {
          templateUrl: "main.html",
          controller: 'mainController'
        })
        .when("/red", {
          templateUrl: "red.html",
          controller: 'redController'
        })
        .when("/green", {
          templateUrl: "green.html",
          controller: 'greenController'
        })       
        .otherwise('/');
  });

  app.controller('mainController',['$scope',function mainController($scope){
    $scope.message='this is main page';
  }]);
  app.controller('redController',['$scope',function mainController($scope){
    $scope.message='this is red page';
  }]);
  app.controller('greenController',['$scope',function mainController($scope){
    $scope.message='this is green page';
  }]);
</script>
</html>

red.html (其他頁(yè)面類似,就不重復(fù)了)

<div style="background: red">
{{message}}
</div>

例子很簡(jiǎn)單,我們就只講下路由的配置:

  1. 使用$routeProvider.when來配置路由的關(guān)系,方法接受兩個(gè)參數(shù),第一個(gè)參數(shù)是url的路徑,第二個(gè)參數(shù)是配置url對(duì)應(yīng)的templateUrl和controller。
  2. $routeProvider.otherwise方法相當(dāng)于default。

路由模塊化

可能你已經(jīng)注意到了上面的都寫在一起,如果項(xiàng)目越來越大,會(huì)不會(huì)很頭疼,那么是不是可以把它模塊化,每個(gè)模塊都有直接的module,controller,config等。模塊依賴的技術(shù)我們之前的module那篇文章已經(jīng)講過,那么就來看下帶有路由的情況下,怎么模塊化。

color.html:

<!DOCTYPE html>
<html>
<head>
  <meta charset="uft-8"/>
  <title></title>
</head>
<script src="script/angular.min.js"></script>
<script src="script/angular-route.min.js"></script>
<script src="red.js"></script>
<script src="green.js"></script>
<script src="main.js"></script>
<body ng-app="color">
<p><a href="#/" rel="external nofollow" rel="external nofollow" >Main</a></p>

<a href="#red" rel="external nofollow" rel="external nofollow" >Red</a>
<a href="#green" rel="external nofollow" rel="external nofollow" >Green</a>

<div ng-view></div>

</body>

<script>
  var app = angular.module("color", ["ngRoute","Module.red","Module.main","Module.green"]);

  app.config(function ($routeProvider) {
    $routeProvider.otherwise('/');
  });
</script>
</html>

可以看到我們的color.html文件是不是很簡(jiǎn)潔,config的路由配置里只有一行$routeProvider.otherwise方法,但是module卻注入了除ngRoute外的三個(gè)module:”Module.red”,”Module.main”,”Module.green”,這其實(shí)是把path對(duì)應(yīng)的路由提取成模塊,使用了專門的js來處理它們,看起來和他們對(duì)應(yīng)的模板相對(duì)應(yīng),我們來看下red.html對(duì)應(yīng)的模塊js:

red.js

angular.module('Module.red', ['ngRoute'])

  .config([
    '$routeProvider',
    function ($routeProvider) {
      $routeProvider.when('/red', {
        templateUrl: 'red.html',
        controller: 'redController'
      });
    }
  ])


  .controller('redController', [
    '$scope',
    function ($scope) {
      $scope.color='red';
      $scope.message = 'This is red page';
    }
  ]);

路由的參數(shù)

那么路由怎么將參數(shù)傳入到模板頁(yè)呢?我們把上面的例子改造一下,通過例子來講解:

main.js

angular.module('Module.main', ['ngRoute'])

  .config([
    '$routeProvider',
    function ($routeProvider) {
      $routeProvider
        .when('/', {
          templateUrl: 'main.html',
          controller: 'mainController'
        });
    }
  ])

  .controller('mainController', [
    '$scope',
    function ($scope) {
      $scope.message = 'This is main page';
      $scope.colors=['blue','yellow','pink'];
    }
  ]);

這里初始化了一個(gè)colors的數(shù)組,作為要傳遞的數(shù)據(jù)。

main.html

{{message}}
<br>
<ul>
  <li ng-repeat="color in colors">
    <a href="#/color/{{color}}" rel="external nofollow" >{{color}}</a>
  </li>
</ul>

通過ng-repeat循環(huán)創(chuàng)建a鏈接,數(shù)組的元素通過鏈接傳入。

colorId.js

angular.module('Module.colorId', ['ngRoute'])

  .config([
    '$routeProvider',
    function ($routeProvider) {
      $routeProvider
        .when('/color/:colorId', {
          templateUrl: 'colorId.html',
          controller: 'colorIdController'
        });
    }
  ])

  .controller('colorIdController', [
    '$scope','$routeParams',
    function ($scope,$routeParams) {
      $scope.color = $routeParams.colorId;
      $scope.message = 'This is ' +$routeParams.colorId +' page';
    }
  ]);

這里定義了一個(gè)colorId的模塊,通過:colorId來匹配傳入的參數(shù),這里匹配到的是數(shù)組中的元素。例如/color/blue,那么匹配到的就是blue。

colorId.html

<div style="background: {{color}}">
  {{message}}
</div>

最后在colorId上呈現(xiàn)出來。

如果是多個(gè)參數(shù)可以直接一一接到后面比如/color/:colorId/:year/:month/:day,和后面的參數(shù)也一一匹配,如/color/pink/2017/3/13。

支持*號(hào):/color/:color/largecode/:largecode*/edit匹配/color/brown/largecode/code/with/slashes/edit的話,color將會(huì)匹配到brown,largecode將匹配到code/with/slashes。

支持?號(hào):/color/:color/largecode/:largecode?/edit可以匹配匹配/color/brown/largecode/code/edit,largecode將會(huì)匹配到code。
/color/:color/largecode/:largecode?/edit可以匹配匹配/color/brown/largecode/edit,largecode將會(huì)匹配到空值。

路由中的resolve

一個(gè)最常見的情景,頁(yè)面跳轉(zhuǎn)時(shí)要加載一些數(shù)據(jù)。有兩種方式可以做到:

  1. 頁(yè)面跳轉(zhuǎn)后加載,通過controller去控制數(shù)據(jù)的加載,如果時(shí)間較長(zhǎng)則顯示一個(gè)loading的界面,數(shù)據(jù)請(qǐng)求成功后再替換成數(shù)據(jù)界面
  2. 頁(yè)面跳轉(zhuǎn)前加載,通過路由的resolve去配置。

個(gè)人更喜歡跳轉(zhuǎn)后加載的方式,因?yàn)楦鼮橛押茫詫?duì)resolve不太感冒,但我們還是講下resolve。

resolve是一個(gè)map對(duì)象:

  1. map的key是可以注入到controller的可選的依賴項(xiàng),如果resolve其中依賴項(xiàng)的返回值是promise,那么在controller初始化之前,路由會(huì)一直等待直到所有的promise都已經(jīng)resolved或者其中的一個(gè)被rejected。如果所有的promise都成功resolved,這些依賴項(xiàng)將可以注入到controller中并同時(shí)觸發(fā)$routeChangeSuccess事件,如果其中的一個(gè)promise是rejected,那么將會(huì)觸發(fā)$routeChangeError事件,并中止路由切換。
  2. map的value如果是個(gè)字符串,那么它會(huì)是一個(gè)service的別名。如果是一個(gè)函數(shù),他的返回值可以被當(dāng)做依賴注入 到controller中,如果返回值是一個(gè)promise,在注入之前必須是resolved的。注意這時(shí)候ngRoute.$routeParams還不可用,如果需要使用參數(shù)則需要使用$route.current.params。

看下例子:

news.html

<html>
<head>
  <meta charset="uft-8"/>
  <title></title>
</head>
<script src="script/angular.min.js"></script>
<script src="script/angular-route.min.js"></script>
<body ng-app="ngst-news">
<div ng-controller="MainController">
  <ul>
    <li ng-repeat="news in newsAarry">
      <a href="#/newsDetail/{{news.id}}" rel="external nofollow" >{{news.title}}</a>
    </li>
  </ul>
  <div ng-view></div>
</div>
</body>

<script src="news.js" charset="UTF-8"></script>
<script src="newsDetail.js" charset="UTF-8"></script>
</html>

news.js

var news = angular.module("ngst-news", ["ngst-newsDetail"]);

news.controller("MainController", ["$scope", function ($scope) {
  $scope.newsAarry = [{"id": "1", "title": "遼寧人大副主任王陽 浙江寧波市長(zhǎng)盧子躍被免職"},
    {"id": "2", "title": "今冬小雨繽紛,荔枝園地濕潤(rùn)壯旺荔枝果樹,下刀環(huán)剝最狠"},
    {"id": "3", "title": "百度任原搜索渠道總經(jīng)理顧國(guó)棟為市場(chǎng)執(zhí)行總監(jiān)"}];
}]);

news頁(yè)面是一個(gè)新聞列表,在列表下面有個(gè)ng-view,點(diǎn)擊新聞列表鏈接下面的ng-view進(jìn)行路由切換。

newsDetails.html

{{message}}

newsDetails.js

var news = angular.module("ngst-newsDetail", ['ngRoute']);

news.config(["$routeProvider",
  function ($routeProvider) {
    $routeProvider.when("/newsDetail/:newsId", {
      templateUrl: 'newsDetail.html',
      controller: 'newsDetailController',
      resolve: {
        content: ['$q', '$timeout', "$route", function ($q, $timeout, $route) {
          var deferred = $q.defer();
          $timeout(function () {
            deferred.resolve('新聞詳情 id=' + $route.current.params.newsId);
          }, 1000);
          return deferred.promise;
        }]
      }
    });
  }])
  .controller("newsDetailController", ['$scope', 'content',
    function ($scope, content) {
      $scope.message = content;
    }]);

這里使用$route.current.params來獲得參數(shù)

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • AngularJS 打開新的標(biāo)簽頁(yè)實(shí)現(xiàn)代碼

    AngularJS 打開新的標(biāo)簽頁(yè)實(shí)現(xiàn)代碼

    本文通過實(shí)例代碼給大家介紹了angularJS 打開新的標(biāo)簽頁(yè)方法,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧
    2017-09-09
  • 詳解angular2采用自定義指令(Directive)方式加載jquery插件

    詳解angular2采用自定義指令(Directive)方式加載jquery插件

    本篇文章主要介紹了詳解angular2采用自定義指令(Directive)方式加載jquery插件 ,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。
    2017-02-02
  • Web前端框架Angular4.0.0 正式版發(fā)布

    Web前端框架Angular4.0.0 正式版發(fā)布

    經(jīng)歷了 6 個(gè) RC 版本之后,Angular 發(fā)布了 4.0.0 正式版。下面這篇文章主要給大家介紹了關(guān)于Web前端框架Angular4.0.0 正式版發(fā)布的相關(guān)資料,文中介紹的非常詳細(xì),需要的朋友們下面來一起看看吧。
    2017-03-03
  • 利用Ionic2 + angular4實(shí)現(xiàn)一個(gè)地區(qū)選擇組件

    利用Ionic2 + angular4實(shí)現(xiàn)一個(gè)地區(qū)選擇組件

    ionic是一個(gè)移動(dòng)端開發(fā)框架,使用hybird技術(shù),只要使用前端開發(fā)技術(shù)就可以開發(fā)出電腦端,安卓端和ios端的站點(diǎn)程序。下面這篇文章主要給大家介紹了關(guān)于利用Ionic2 + angular4實(shí)現(xiàn)一個(gè)地區(qū)選擇組件的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-07-07
  • Angular2環(huán)境搭建具體操作步驟(推薦)

    Angular2環(huán)境搭建具體操作步驟(推薦)

    下面小編就為大家?guī)硪黄狝ngular2環(huán)境搭建具體操作步驟(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-08-08
  • AngularJS基礎(chǔ)學(xué)習(xí)筆記之簡(jiǎn)單介紹

    AngularJS基礎(chǔ)學(xué)習(xí)筆記之簡(jiǎn)單介紹

    AngularJS 不僅僅是一個(gè)類庫(kù),而是提供了一個(gè)完整的框架。它避免了您和多個(gè)類庫(kù)交互,需要熟悉多套接口的繁瑣工作。它由Google Chrome的開發(fā)人員設(shè)計(jì),引領(lǐng)著下一代Web應(yīng)用開發(fā)。也許我們5年或10年后不會(huì)使用AngularJS,但是它的設(shè)計(jì)精髓將會(huì)一直被沿用。
    2015-05-05
  • AngularJS實(shí)現(xiàn)表單驗(yàn)證功能詳解

    AngularJS實(shí)現(xiàn)表單驗(yàn)證功能詳解

    這篇文章主要為大家詳細(xì)介紹了AngularJS實(shí)現(xiàn)表單驗(yàn)證功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • angularJS+requireJS實(shí)現(xiàn)controller及directive的按需加載示例

    angularJS+requireJS實(shí)現(xiàn)controller及directive的按需加載示例

    本篇文章主要介紹了angularJS+requireJS實(shí)現(xiàn)controller及directive的按需加載示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-02-02
  • AngularJS中scope的綁定策略實(shí)例分析

    AngularJS中scope的綁定策略實(shí)例分析

    這篇文章主要介紹了AngularJS中scope的綁定策略,結(jié)合實(shí)例形式簡(jiǎn)單分析了AngularJS scope的三種綁定模式的使用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2017-10-10
  • AngularJS實(shí)現(xiàn)分頁(yè)顯示數(shù)據(jù)庫(kù)信息

    AngularJS實(shí)現(xiàn)分頁(yè)顯示數(shù)據(jù)庫(kù)信息

    這篇文章主要為大家詳細(xì)介紹了AngularJS實(shí)現(xiàn)分頁(yè)顯示數(shù)據(jù)庫(kù)信息效果的相關(guān)資料,感興趣的小伙伴們可以參考一下
    2016-07-07

最新評(píng)論

洛宁县| 武安市| 体育| 峡江县| 巴林左旗| 柳江县| 柘城县| 鱼台县| 饶阳县| 怀宁县| 黔江区| 泰宁县| 九江县| 南川市| 大港区| 武强县| 密山市| 巢湖市| 淮北市| 平湖市| 拉萨市| 永靖县| 北京市| 谢通门县| 无锡市| 衡南县| 泰来县| 嘉鱼县| 抚顺县| 蒙自县| 宣汉县| 阿拉善右旗| 阳东县| 调兵山市| 凤城市| 景宁| 嘉义市| 和平县| 玛曲县| 克什克腾旗| 邢台市|