gpt4 book ai didi

javascript - AngularJs - 如何在不使用 ng-touch 或 ng-click 的情况下检测移动设备中的点击事件?

转载 作者:行者123 更新时间:2023-12-05 06:48:08 24 4
gpt4 key购买 nike

下面的代码在桌面上检测到click,但在移动设备上,touch事件在尝试滚动元素时被触发,如何防止这个touch 事件,只检测 tap 事件。我不允许使用 ng-touchng-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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com