下面的代码在桌面上检测到click
,但在移动设备上,touch
事件在尝试滚动元素时被触发,如何防止这个touch
事件,只检测 tap
事件。我不允许使用 ng-touch
或 ng-click
。
我尝试了以下方法,但这些方法似乎不起作用。需要对以下代码进行哪些更改?
What's the best way to detect a 'touch screen' device using JavaScript?
onClick not working on mobile (touch)
What is the most reliable way to detect clicks (or touches) on both desktop and mobile devices?
(function() {
'user strict';
angular.module('myModule').directive('myDirective',function(){
return{
restrict:'A',
scope:{
id: '=myId',
pm: '=myPm'
},
link:function(scope,element,attrs){
element.on('click touchend',funtion(event){
console.log("clicked");
});
}
}:
});
}());
最佳答案
使用 onclick="myFunction()"或使用 ng-click 它肯定会起作用 在 HTML 中—— ng-click="xyz(参数)" 如下所示::
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>ng-click in AngularJs</title>
<script src="https://code.angularjs.org/1.4.1/angular.js"></script>
<script>
var app = angular.module('myApp', []);
app.controller('ngClickCtrl', ["$scope", function ($scope) {
$scope.domain = "stackoverflow.com";
$scope.IsDisplay = false;
$scope.clickMe = function (clicked) {
alert(" My Click function is called.");
$scope.IsDisplay = clicked == true ? false : true;
};
}]);
</script>
</head>
<body ng-app="myApp">
<div ng-controller="ngClickCtrl">
<div><h2>ng-click in AngularJs</h2> </div>
<div>
Domain name: <input type="text" ng-model="domain"> <button type="button" ng-click="clickMe(IsDisplay)">NG-CLICK</button>
</div>
<div>
<div ng-show="IsDisplay"> My Click function is called. <br /><h3 style="color:green;">testing ng-click events: {{domain}} </h3></div>
</div>
</div>
</body>
</html>
关于javascript - AngularJs - 如何在不使用 ng-touch 或 ng-click 的情况下检测移动设备中的点击事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66899592/