在运用angular开发移动端的应用时,发现它并没有将ng-click做兼容,在移动端使用ng-click事件仍然会有300ms延迟。后来发现angular有一个专门针对移动端的模块:angular-touch.js,其中对ng-click做了兼容性处理,以下为该模块中部分内容:

  /**
* @ngdoc method
* @name $touchProvider#ngClickOverrideEnabled
*
* @param {boolean=} enabled update the ngClickOverrideEnabled state if provided, otherwise just return the
* current ngClickOverrideEnabled state
* @returns {*} current value if used as getter or itself (chaining) if used as setter
*
* @kind function
*
* @description
* Call this method to enable/disable {@link ngTouch.ngClick ngTouch's ngClick directive}. If enabled,
* the default ngClick directive will be replaced by a version that eliminates the 300ms delay for
* click events on browser for touch-devices.
*
* The default is `false`.
*
*/
var ngClickOverrideEnabled = false;
var ngClickDirectiveAdded = false;
// eslint-disable-next-line no-invalid-this
this.ngClickOverrideEnabled = function(enabled) {
if (angular.isDefined(enabled)) { if (enabled && !ngClickDirectiveAdded) {
ngClickDirectiveAdded = true; // Use this to identify the correct directive in the delegate
ngTouchClickDirectiveFactory.$$moduleName = 'ngTouch';
$compileProvider.directive('ngClick', ngTouchClickDirectiveFactory); $provide.decorator('ngClickDirective', ['$delegate', function($delegate) {
if (ngClickOverrideEnabled) {
// drop the default ngClick directive
$delegate.shift();
} else {
// drop the ngTouch ngClick directive if the override has been re-disabled (because
// we cannot de-register added directives)
var i = $delegate.length - 1;
while (i >= 0) {
if ($delegate[i].$$moduleName === 'ngTouch') {
$delegate.splice(i, 1);
break;
}
i--;
}
}
return $delegate;
}]);
}
ngClickOverrideEnabled = enabled;
return this;
} return ngClickOverrideEnabled;
};

  其中说明引用该模块,可以将ng-click替换为兼容移动端的点击事件,具体代码如下:

angular.module('myapp',['ngTouch'])
.config(['$touchProvider',function ($touchProvider) {
$touchProvider.ngClickOverrideEnabled(true);
}])

更新于2017.11.2

  后来发现angular-touch模块覆盖原本的ng-click之后出现了点击穿透的BUG,并且a标签和button的点击事件300ms延迟并没有消失,后来还是选择用fastclick.js好了,在angular中引入fastclick也非常简单,并且使用fastclick之后,a标签的点击事件的延迟也消失了。

  一共就两行代码:

<script src="./lib/fastclick.min.js"></script>  //引入fastclick文件

myapp.config("xxxx",function(){
  FastClick.attach(document.body);//在配置中加上这句话
})

angularJS在移动端的点击事件延迟问题的更多相关文章

  1. jquery关于移动端的点击事件解析

    jquery关于移动端的点击事件解析 如果用click来写 你连续点几下 会没有反应 响应是非常慢的 所以移动端必须用touchstart代替click来写

  2. 【技术博客】移动端的点击事件与Sticky Hover问题

    目录 移动端的点击事件与Sticky Hover问题 TL;DR 前言 问题描述 背景 实现方式 问题 关于移动端浏览器的点击事件 初次发现问题后各种解决尝试:从点击事件本身下手 cursor: po ...

  3. Angularjs 中使用指令绑定点击事件

    项目中,模板中的菜单是jQuery控制的,在Angularjs中就运行不到了,因为菜单项是ng-repeat之后的. 如html <ul id="main-menu"> ...

  4. 移动端300ms点击事件的延迟

    移动端click事件300ms延迟 移动端click事件300ms的延迟在目前看来,已经是老生常谈了. 以下内容,我会在参考资源的基础上谈谈我对移动端click事件300ms延迟的一些理解.本人愚昧, ...

  5. ios移动端浏览器点击事件失效的解决方案

    点击事件失效的原因可能是因为,你用了事件代理了, 比如这样 $(document).on("click",".fd",function(){ }) 这段代码在安 ...

  6. 移动端触屏click点击事件延迟问题,以及tap的解决方案

    在移动端 触屏click事件虽然也会响应,但是总感觉是有延迟,一直听说click事件在手机上有200~300毫秒的延迟问题,亲自测了一下,在pc端模拟手机的话是测不出来的,但是用手机测试时发现延迟非常 ...

  7. 移动端的传统click事件延迟和点透现象

    一.场景描述: 1.A/B两个层上下z轴重叠. 2.上层的A点击后消失或移开.(这一点很重要) 3.B元素本身有默认click事件(如a标签) 或 B绑定了click事件. 在以上情况下,点击A/B重 ...

  8. 移动端IOS点击事件失效解决方案

    解决方案 解决办法有 4 种可供选择: 1 将 click 事件直接绑定到目标元素(即 .target)上 2 将目标元素换成 <a> 或者 button 等可点击的元素 3 将 clic ...

  9. HTML5 移动端 自定义点击事件

    /* 封装的TAP事件 */ (function () { /** * IOS 和 PC 端 只需要创建一次就能一直使用 * Android 手机 每次使用的时候都需要从新创建 */ function ...

随机推荐

  1. DataGrid 显示选中的item

    Datagrid或者listview 中想要把相应的项 滚动到当前可见的位置, 必须满足2个条件: 1) 必须去掉虚拟化      VirtualizingStackPanel.IsVirtualiz ...

  2. spring boot 学习三:OAuth2 认证

    1:  代码地址: https://github.com/liufeiSAP/uaa-zuul 2:     安装: postgres 下载 https://www.openscg.com/bigsq ...

  3. ng2父子模块数据交互

    一.父模块向子模块传值 //父html <my-child [childdata]="parentdata"></my-child> 其中,my-child ...

  4. There&nbsp;is&nbsp;no&nbsp;resul…

    There is no result type defined for type 'json' mapped with name 'success'. 这个错误是json初学者很容易遇到的错误:现在把 ...

  5. 1. docker安装

    前提 系统:我这边都使用虚拟机安装的CentOS7,具体安装可以参考:Windows安装Linux虚拟机(CentOS7) yum:推荐更新下yum:yum update;我们这边CentOS7自带d ...

  6. JAVA基础--IO输入输出(File使用)17

    一. File中的方法 1.  列举方法 /* * 获取指定目录以及子目录下的所有文件和文件夹 */ public class ListFilesDemo { public static void m ...

  7. JS实现页面刷新方法

    下面介绍全页面刷新方法:有时候可能会用到 window.location.reload()刷新当前页面. parent.location.reload()刷新父亲对象(用于框架) opener.loc ...

  8. linux安装AWStats业务数据分析工具

    Awstats是一个非常简洁而且强大的统计工具.它可以统计您站点的如下信息:一:访问量,访问次数,页面浏览量,点击数,数据流量等精确到每月.每日.每小时的数据二:访问者国家.访问者IP.操作系统.浏览 ...

  9. PhpStorm之三种视图模式

    全屏模式 允许你使用整个屏幕编写代码,在视图中移除了所有菜单,同样也移除了操作系统控制栏.然而,你可以继续使用右键菜单和快捷键.当你把鼠标悬停到屏幕顶部时仍然可以看到主菜单. 演示模式 这个模式和全屏 ...

  10. Python学习笔记(随机数)

    random模块的作用是产生随机数. import random num = random.randint(1,100) random.randint(a, b)可以生成一个a到b间的随机整数,包括a ...