我有一个元素可以激活ng-mousedown
和ng-mouseup
上的函数。但是,它在触摸屏上不起作用,是否有ng-touchstart
和ng-touchend
这样的指令?
答案 0 :(得分:14)
有一个模块:https://docs.angularjs.org/api/ngTouch
但你也可以为事件编写自己的指令:
<!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>
&#13;
答案 1 :(得分:8)