[英]AngularJS: ng-click and select text
在我的應用程序中,我在點擊元素后使用了一些邏輯。
例如:
HTML
<h3 ng-click="showAlert('text')">this is text! (try to click and select)</h3>
JS
$scope.showAlert = function(text) {
console.log('click!');
};
當我點擊元素時:一切都好。 一切都按預期完成。
但!
為什么,當我嘗試選擇文字時,我的活動也被解雇了?
如何跳過選擇事件?
plunker: 點擊
您可以在mousedown
和mouseup
之間獲得時間,以確定是單擊還是正在選擇文本。
像這樣: 演示
(function(){
angular.module("app", []);
angular.module("app")
.directive("noclick", function(){
return {
restrict: "A",
link: function(scope, element, attr){
scope.time= Date.now();
element.on("mousedown mouseup", function(e){
if(e.type == "mousedown"){
scope.time= Date.now();
}
if(e.type == "mouseup"){
if(Date.now() - scope.time> 300){
return false;
}else{
console.log("clicked");
}
}
});
}
}
});
})(window, angular);
注意:我正在使用針對此案例的指令。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.