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呢。

 

angular 滚动的更多相关文章

  1. angular实现的文字上下无缝滚动

    最近在学习angularJs,业余时间随便写了一个文字上下无缝滚动的例子,主要写了一个小小的指令. css代码:主要控制样式 <style type="text/css"&g ...

  2. Angular: 使用 RxJS Observables 来实现简易版的无限滚动加载指令

    我使用 angular-cli 来搭建项目. ng new infinite-scroller-poc --style=scss 项目生成好后,进入 infinite-scroller-poc 目录下 ...

  3. Angular关于$anchorScroll的定位滚动

    以下是实现定位滚动的代码: <!DOCTYPE html> <html lang="en" ng-app="app"> <head ...

  4. [转]angular 监听窗口滚动

    本文转自:https://blog.csdn.net/ittvibe/article/details/80060801 转自:http://brianflove.com/2016/10/10/angu ...

  5. 【angularjs】使用angular搭建项目,滚动距离

    常用方法 滚动到顶部:$ionicScrollDelegate.scrollTop();或者$ionicScrollDelegate.$getByHandle('视图句柄').scrollTop(); ...

  6. angular浏览器滚动条滚动到指定element 触发事件

    angular.module('app').directive('ScrollTrigger', () => { return { restrict: "A", link:f ...

  7. 使用 Angular 和 RxJS 实现的无限滚动加载

    无限滚动加载应该是怎样的? 无限滚动加载列表在用户将页面滚动到指定位置后会异步加载数据.这是避免寻主动加载(每次都需要用户去点击)的好方法,而且它能真正保持应用的性能.同时它还是降低带宽和增强用户体验 ...

  8. Angular 回到顶部 滚动到特定的页面位置

    $timeout(function() { // $location.hash('bottom'); // $anchorScroll(); // var a=angular.element(&quo ...

  9. Angular移除不必要的$watch之性能优化

    双向绑定是Angular的核心概念之一,它给我们带来了思维方式的转变:不再是DOM驱动,而是以Model为核心,在View中写上声明式标签.然后,Angular就会在后台默默的同步View的变化到Mo ...

随机推荐

  1. 解决UIButton 连续点击重复响应事件问题

    经常会遇到重复点击某个按钮 事件被响应多次的情景, 有时候可能对程序本身并没有什么影响 , 可有时候偏偏需要限制button响应事件直接的间隔 . 方法一 : 标记 1 . 利用空闲enable属性来 ...

  2. div模拟table,可实现左右高度同增长(html布局)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. 大家把做的公祭日的ps上传哦

    上传时图片保存为JPG,写上自己的学号,说说自己的创作构思

  4. Mvc 提交表单的4种方法全程详解

    一,MVC  HtmlHelper方法 Html.BeginForm(actionName,controllerName,method,htmlAttributes){} BeginRouteForm ...

  5. KETTLE 配置资源库

    KETTLE 是一款开源的ETL工具,通过图形界面进行设计,可以对数据进行转换.设计好的文件分为两类,一类是trans,一类是job,这些文件可以存储到文件系统中.   也可以存储到数据库中.   如 ...

  6. 三星四核RP4412开发板的root问题

    问:荣品四核RP4412开发板的板子是root权限吗? 或者怎么root? 答:su . android 我们提供的是root用户 问:root以后的授权管理器没法运行. 我需要root,我要在and ...

  7. nsmutableset

    // //  main.m //  nsmutableset // //  Created by 博博 on 16/1/11. //  Copyright (c) 2016年 com.bb. All ...

  8. daydayup2 codeforces143C

    题意:给你n= (A - 1) × (B - 2) × (C - 2),求A*B*C的最大值和最小值 思路:要用好的姿势暴力 #include "stdio.h" #include ...

  9. SET ANSI_NULLS ON ……

    SET QUOTED_IDENTIFIER ON   SET ANSI_NULLS ON    SET QUOTED_IDENTIFIER ON  GO  是什么意思?    语法  SET QUOT ...

  10. ROS机器人语音交互(一)

    语音交互早期已经广泛应用在手机端,电脑端,随着技术的成熟,接口逐渐开放,ROS上老外搞的开源语音识别只支持英文,识别率还低. 国内语音识别技术已经相当成熟稳定.感谢ros小课堂的讲解,解决了自己的疑惑 ...