Angularjs中的ng-touchstart和ng-touchend [英] ng-touchstart and ng-touchend in Angularjs
本文介绍了Angularjs中的ng-touchstart和ng-touchend的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个元素可以在 ng-mousedown
和 ng-mouseup
上触发函数。但是,它在触摸屏上不起作用,是否有任何指令,如 ng-touchstart
和 ng-touchend
?
I have an element which fires functions on ng-mousedown
and ng-mouseup
. However, It doesn't work on touch screen, is there any directive like ng-touchstart
and ng-touchend
?
推荐答案
有一个模块: https://docs.angularjs.org/api/ngTouch
但您也可以为事件编写自己的指令:
But you can write your own directives for events too:
<!doctype html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.js"></script>
</head>
<body ng-app="plunker">
<div ng-controller="MainCtrl">
<div my-touchstart="touchStart()" my-touchend="touchEnd()">
<span data-ng-hide="touched">Touch Me ;)</span>
<span data-ng-show="touched">M-m-m</span>
</div>
</div>
<script>
var app = angular.module('plunker', []);
app.controller('MainCtrl', ['$scope', function($scope) {
$scope.touched = false;
$scope.touchStart = function() {
$scope.touched = true;
}
$scope.touchEnd = function() {
$scope.touched = false;
}
}]).directive('myTouchstart', [function() {
return function(scope, element, attr) {
element.on('touchstart', function(event) {
scope.$apply(function() {
scope.$eval(attr.myTouchstart);
});
});
};
}]).directive('myTouchend', [function() {
return function(scope, element, attr) {
element.on('touchend', function(event) {
scope.$apply(function() {
scope.$eval(attr.myTouchend);
});
});
};
}]);
</script>
</body>
</html>
这篇关于Angularjs中的ng-touchstart和ng-touchend的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文