AngularJs $anchorScroll、$controller、$document
$anchorScroll
根據HTML5的規則,當調用這個函數時,它檢查當前的url的hash值并且滾動到相應的元素。
監聽$location.hash()并且滾動到url指定的錨點的地方。可以通過$anchorScrollProvider.disableAutoScrolling()禁用。
依賴:$window?? $location?? $rootScope
使用:$anchorScroll();
使用代碼:
#id {height:500px;}#bottom {margin-top:1500px;} <div ng-app="Demo" ng-controller="testCtrl as ctrl"><div id="top" ng-click="ctrl.gotoBottom()">跳到底部</div><div id="bottom" ng-click="ctrl.gotoTop()">跳到頂部</div></div> (function () {angular.module("Demo", []).controller("testCtrl",["$location", "$anchorScroll",testCtrl]);function testCtrl($location,$anchorScroll){this.gotoTop = function () {$location.hash("top");$anchorScroll();};this.gotoBottom = function () {$location.hash("bottom");$anchorScroll();};};}());$controller
$controller負責實例化控制器。
這只是個簡單的$injector調用,但為了以前版本的這個服務能被覆蓋而被提取進一個服務。
依賴:$injector
使用:$controller(constructor,locals);
constructor:如果調用了一個函數,那么這個函數被認為是控制器構造函數。否則,它被認為是一個使用以下步驟檢索控制器的構造函數的字符串:
1.檢查控制器是否在$controllerProvider注冊并命名。
2. 檢查當前作用域上的字符串是否返回一個構造函數
3.在全局window對象上檢查構造器。
locals:Object,將需要調用的控制器注冊到當前控制器。
使用代碼:
(function () {angular.module("Demo", []).controller("demoCtrl",["$scope",demoCtrl]).controller("testCtrl",["$controller","$scope",testCtrl]);function demoCtrl($scope){$scope.print = function () {console.log("print");};this.prt = function () {$scope.print();};};function testCtrl($controller,$scope){var ctrl = $controller("demoCtrl",{$scope:$scope});ctrl.prt(); // print };}());$document
一個jQuery或jqlite包裝了的瀏覽器window.document對象。
依賴:$window
使用代碼:
<!doctype html> <html> <head><meta charset="utf-8"><script src='angular.js'></script><title>title-$document</title> </head> <body><div ng-app="Demo" ng-controller="testCtrl as ctrl"></div><script>(function () {angular.module("Demo", []).controller("testCtrl",["$document",testCtrl]);function testCtrl($document){var $title = $document[0].title;//title-$documentvar title = angular.element(window.document)[0].title;//title-$documentvar v = $document[0] === window.document;//true };}());</script> </body> </html>這兩天被$animate和$interpolate還有$http給折騰的心累啊,有一小部分代碼還沒測出來,所以先把這三個內容少點的整合到一篇文章先總結了先。明天看看花點時間把那三個給寫完整吧,估計需要分三篇文章來記錄$animate、$interpolate和$http呢。
轉載于:https://www.cnblogs.com/ys-ys/p/4982542.html
總結
以上是生活随笔為你收集整理的AngularJs $anchorScroll、$controller、$document的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jeesite使用心得(二)
- 下一篇: team geek