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-$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呢。
标签:
AngularJs
, AngularJs Api
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?