ng-mousedown
我有一个在和上触发功能的元素ng-mouseup
。但是,它在触摸屏上不起作用,是否有类似ng-touchstart
and的指令ng-touchend
?
问问题
37915 次
3 回答
13
有一个模块: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>
于 2014-10-03T09:51:54.347 回答
8
于 2015-02-03T13:23:32.587 回答
2
我们提出的版本使用 $parse(),这是 $eval() 内部使用的。我们特别想使用单个指令来处理 mousedown 和 touchstart 事件,但是以角度方式,因此我们可以包含角度样式表达式。
像这样:
angular.module("ngStudentselect", []).directive('ngStudentselect', ['$parse', '$timeout', '$rootElement',
function($parse, $timeout, $rootElement) {
return function(scope, element, attr) {
var clickHandler = $parse(attr.ngStudentselect);
element.on('mousedown touchstart', function(event) {
scope.$apply(function() {
clickHandler(scope, {$event: event});
});
});
};
}]);
这是 Angular 的 ngClick 指令的精简版。
于 2015-08-26T22:58:03.113 回答