[英]separate click event from focus event in Angular.js
I want to add a click handler to an ng-focus
callback, to start listening to all future click events. 我想向
ng-focus
回调添加点击处理程序,以开始侦听所有将来的点击事件。 But when focus is initiated by a click, the click handler fires immediately as well. 但是,当通过单击启动焦点时,单击处理程序也会立即触发。 I can solve the problem with a
$timeout
, but this is a hack and it goes against everything I stand for. 我可以用
$timeout
解决问题,但这是一个hack,它违背了我所代表的一切。 What have I become?! 我变成了什么?
My HTML: 我的HTML:
<input type="text" name="q" ng-focus="inputFocus($event)" />
This .js works (this is inside a directive's link function): 这个.js 可以工作 (这在指令的链接函数内部):
scope.inputFocus = function (e) {
scope.displayHistory = true;
$timeout(function () {
$document.on('click', hideHistory);
}, 200)
};
function hideHistory() {
scope.displayHistory = false;
scope.$digest();
$document.off('click')
};
I want the following to work, but the problem is that the click callback fires immediately. 我希望执行以下操作,但是问题是click回调会立即触发。 As you might expect, if I tab to this field, the click callback does not get called.
如您所料,如果我跳至该字段,则不会调用click回调。 But if the focus event is triggered by a click,
hideHistory
gets called. 但是,如果焦点事件是通过单击触发的,
hideHistory
调用hideHistory
。
scope.inputFocus = function (e) {
scope.displayHistory = true;
$document.on('click', hideHistory);
};
I tried calling e.stopPropagation()
and e.stopImmediatePropagation()
but these solutions were not effective either. 我尝试调用
e.stopPropagation()
和e.stopImmediatePropagation()
但这些解决方案均无效。
Have you tried e.preventDefault();
您是否尝试过
e.preventDefault();
? ?
Anyway you can provide a fiddle to illustrate exactly what's going on? 无论如何,您可以提供一个小提琴来确切说明正在发生的事情?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.