AngularJS模塊學習 1 Anchor Scroll

好記性不如爛筆頭。。。算是作個筆記,第一個從Anchor scroll開始吧, 喝杯咖啡的時間就能看完,長的不看 :)html

  • $anchorScroll()用於跳轉至定義ID;
  • $location對象的hash()方法會替換當前url做爲hash鍵;
  • $anchorScroll()讀取並跳轉至ID處。

下面簡單的例子,這裏是輸出結果:app

源碼 index.html--11行,標示了的跳轉ID:url

 1 <!DOCTYPE html>
 2 <html ng-app="app">
 3 <head>
 4   <script src="angular.min.js"></script>
 5   <script src="app.js"></script>
 6   <meta charset="utf-8">
 7 </head>
 8 
 9 <body ng-controller="MockController">
10  <button ng-repeat="(key, value) in numbers" ng-click="jumper(key)"> {{key}} </button>
11  <div ng-repeat="(key, value) in numbers" id="{{key}}">
12     <h1>{{key}}</h1>
13     <ol>
14       <ul ng-repeat="item in value"> {{item}} </ul>
15     </ol>
16  </div>
17 </body>
18 </html>

app.jsspa

 1 var demoApp = angular.module("app",[])
 2 .controller("MockController",
 3     function ($scope, $location, $anchorScroll) {
 4         $scope.numbers = {
 5             "天然數":["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20"],
 6             "質數":["2","3","5","7","11","13", "17", "19", "23", "29"]
 7     };
 8 
 9     $scope.jumper = function(key){
10         $location.hash(key);
11         $anchorScroll();
12     }
13 });
相關文章
相關標籤/搜索