标签:
野兽的ng api学习 -- $anchorScroll、$controller、$document
$anchorScroll
根据HTML5的规则,当调用这个函数时,它检查当前的url的hash值并且滚动到相应的元素。
监听$location.hash()并且滚动到url指定的锚点的地方。可以通过$anchorScrollProvider.disableAutoScrolling()禁用。
依赖:$window $location $rootScope
使用:$anchorScroll();
使用代码:
<style>
#id {
height:500px;
}
#bottom {
margin-top:1500px;
}
</style>
<div id="top" ng-click="gotoBottom()">跳到底部</div>
<div id="bottom" ng-click="gotoTop()">跳到顶部</div>
<script>
angular.module("Demo", [])
.controller("demoCtrl", ["$scope", "$location", "$anchorScroll",
function ($scope,$location,$anchorScroll) {
$scope.gotoTop = function () {
$location.hash("top");
$anchorScroll();
};
$scope.gotoBottom = function () {
$location.hash("bottom");
$anchorScroll();
};
}])
</script>
$controller
$controller负责实例化控制器。
这只是个简单的$injector调用,但为了以前版本的这个服务能被覆盖而被提取进一个服务。
依赖:$injector
使用:$controller(constructor,locals);
constructor:如果调用了一个函数,那么这个函数被认为是控制器构造函数。否则,它被认为是一个使用以下步骤检索控制器的构造函数的字符串:
1.检查控制器是否在$controllerProvider注册并命名。
2. 检查当前作用域上的字符串是否返回一个构造函数
3.在全局window对象上检查构造器。
locals:在本地注册controller。
使用代码:
angular.module("Demo", [])
.controller("testCtrl", ["$scope", function ($scope) {
var self = this;
$scope.print = function () {
console.log("print");
};
self.prt = function () {
$scope.print();
};
return self;
}])
.controller("demoCtrl", ["$scope", "$controller", function ($scope, $controller) {
var ctrl = $controller("testCtrl", { $scope: $scope });
ctrl.prt();//print
}])
$document
一个jQuery或jqlite包装了的浏览器window.document对象。
依赖:$window
使用代码:
<html>
<head>
<script src=‘angular.js‘></script>
<title>title-$document</title>
</head>
<body>
<div id="app" ng-app="Demo" ng-controller="demoCtrl">
</div>
<script>
angular.module("Demo", [])
.controller("demoCtrl", ["$scope", "$document", function ($scope, $document) {
var $title = $document[0].title;//title-$document
var title = angular.element(window.document)[0].title;//title-$document
var v = $document[0] === window.document;//true
}])
</script>
</body>
</html>
这两天被$animate和$interpolate还有$http给折腾的心累啊,有一小部分代码还没测出来,所以先把这三个内容少点的整合到一篇文章先总结了先,困了... 睡觉 ,明天看看花点时间把那三个给写完整吧 估计需要分三篇文章来记录$animate、$interpolate和$http呢。
野兽的Angular Api 学习、翻译及理解 - - $anchorScroll、$controller、$document
标签:
原文地址:http://www.cnblogs.com/ys-ys/p/4982542.html