angular touch 事件指令 #angular #javascript 发表于 2016-08-22 | 分类于 angular | | 阅读次数 touch 指令代码12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152"use strict";angular.module("ngTouch", []).directive("ngTouchstart", function () { return { controller: function ($scope, $element, $attrs) { $element.bind('touchstart', onTouchStart); function onTouchStart(event) { var method = $element.attr('ng-touchstart'); $scope.$event = event; $scope.$apply(method); }; } };}).directive("ngTouchmove", function () { return { controller: function ($scope, $element, $attrs) { $element.bind('touchstart', onTouchStart); function onTouchStart(event) { event.preventDefault(); $element.bind('touchmove', onTouchMove); $element.bind('touchend', onTouchEnd); }; function onTouchMove(event) { var method = $element.attr('ng-touchmove'); $scope.$event = event; $scope.$apply(method); }; function onTouchEnd(event) { event.preventDefault(); $element.unbind('touchmove', onTouchMove); $element.unbind('touchend', onTouchEnd); }; } };}).directive("ngTouchend", function () { return { controller: function ($scope, $element, $attrs) { $element.bind('touchend', onTouchEnd); function onTouchEnd(event) { var method = $element.attr('ng-touchend'); $scope.$event = event; $scope.$apply(method); }; } };}); 使用方法1234567891011121314151617181920212223242526272829303132<!DOCTYPE html><html ng-app="app"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"> <title>angular的touch事件</title> <script type="text/javascript" src="../../js/angular.min-1.4.3.js"></script> <script type="text/javascript" src="../../js/ngTouch.js" ></script> <script type="text/javascript"> var app=angular.module("app",["ngTouch"]); app.controller("touchCtrl",function($scope){ $scope.touchStart=function(){ alert("touchStart"); } $scope.touchMove=function(){ console.log("touchMove"); } $scope.touchEnd=function(){ alert("touchEnd"); } }); </script> </head> <body> <div ng-controller="touchCtrl"> <button ng-touchstart="touchStart()">touchStart</button> <button ng-touchmove="touchMove()">touchMove</button> <button ng-touchend="touchEnd()">touchEnd</button> </div> </body></html>