使用AngularJS實現可伸縮的頁面切換的方法,angularjs頁面切換

來源:互聯網
上載者:User

使用AngularJS實現可伸縮的頁面切換的方法,angularjs頁面切換

AngularJS 1.2 通過引入基於純CSS class的切換和動畫,在一個單頁面應用建立頁面到頁面的切換變得更加的容易。只需要使用一個ng-view,讓我們來看一下,一個引入眾多的不同切換的可伸縮方法,以及指定的每個頁面如何切入和切出。

示範: http://embed.plnkr.co/PqhvmW/preview

首先,標記:
 

 <div class="page-container">  <div ng-view class="page-view" ng-class="pageAnimationClass"> </div> </div>
既然ng-view使用進入/離開動畫,那麼就能簡單地在DOM裡使用兩個 ng-view 元素來進行新視圖切入和舊視圖切出。因此,我們在使用相對定位的 page-container 元素裡,使用絕對位置建立了ng-view,從而支援任意一種定位切換。

'go' 方法

在單頁面應用裡,我們仍想啟用通過URL導航和確保瀏覽器的回退和下一步按鈕如預期的功能。所以一旦我們在$routeProvider設好我們的路由,模板,控制器(可選的解析),我們可以在一個 ng-click 裡使用一個相對路徑來直接切換頁面:
 

 <a ng-click="/page2">Go to page 2</a>

那樣也可以工作,但是我們需要在ng-view 寫入程式碼指定切換一個class 。以此代替,讓我們在 $rootScope 上建立一個 'go' 方法,可以讓我們指定一個路徑和一個像這樣的切換:
 

 <a ng-click="go('/page2', 'slideLeft')">Go to page 2</a>

這是我們 $rootScope 'go' 方法:
 

$rootScope.go = function (path, pageAnimationClass) {   if (typeof(pageAnimationClass) === 'undefined') { // Use a default, your choice    $rootScope.pageAnimationClass = 'crossFade';  }       else { // Use the specified animation    $rootScope.pageAnimationClass = pageAnimationClass;  }   if (path === 'back') { // Allow a 'back' keyword to go to previous page    $window.history.back();  }       else { // Go to the specified path    $location.path(path);  }};

現在,任何你第二個參數指定的 切換類 將會添加到 ng-view 並且 go 方法將會用指定的第一個參數改變頁面路徑。

切換類

接下來要做的就是建立一個任意數量的切換類,並使用 ngAnimate module 提供的鉤子,例如:
 

/* slideLeft */.slideLeft {  transition-timing-function: ease;  transition-duration: 250ms;} .slideLeft.ng-enter {  transition-property: none;  transform: translate3d(100%,0,0);} .slideLeft.ng-enter.ng-enter-active {  transition-property: all;  transform: translate3d(0,0,0);} .slideLeft.ng-leave {  transition-property: all;  transform: translate3d(0,0,0);} .slideLeft.ng-leave.ng-leave-active {  transition-property: all;  transform: translate3d(-100%,0,0);}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.