简体   繁体   English

忽略某些元素的ng-blur事件

[英]Ignore ng-blur event for certain elements

I have a search field with suggestion box below it. 我在其下方有一个带有建议框的搜索字段。 When I type in the search field, the suggestions appear below it. 当我在搜索字段中键入内容时,建议将显示在其下方。 When I click somewhere outside the field, I want the suggestion box to disappear. 当我单击字段外的某个位置时,我希望建议框消失。 And I want to be able to click on a suggestion and perform a search. 我希望能够单击建议并执行搜索。

<input ng-blur="suggestionBox.hide()" ng-model='someModel' />

<div class='suggestion-box'>
<ul>
<li ng-repeat="suggestion in suggestions" ng-click="someFunction()">suggestion.name</li>
</ul>
</div>

The problem is that because of the ng-blur , the ng-click is not triggered - the suggestion box becomes hidden but the click event doesn't trigger. 问题是,因为的ng-blur ,在ng-click未被触发-意见箱被隐藏,但Click事件不会触发。 I tried ng-click="$event.preventDefault(); someFunction()" but it doesn't work. 我尝试了ng-click="$event.preventDefault(); someFunction()"但是它不起作用。 I tried to set timeout on the hide() method, and it works but I have to set a big timeout like 200ms and generally I think that is a bad solution. 我试图在hide()方法上设置超时,它可以工作,但是我必须设置一个200ms的大超时,通常我认为这是一个不好的解决方案。

edit: http://plnkr.co/edit/9BR7Sv2502S87HO56Ofp?p=preview 编辑: http //plnkr.co/edit/9BR7Sv2502S87HO56Ofp?p = preview

One thing I've done is provided ng-mousedown and ng-mouseup functions on items i want to click on while circumventing the onBlur. 我要做的一件事是在我想绕开onBlur时要单击的项目上提供ng-mousedown和ng-mouseup函数。 I haven't found any other way around it. 我没有找到其他解决方法。 In my case it was a sort of a cancel edit button. 就我而言,这是一种取消编辑按钮。

  1. On mousedown i set a flag "inWhateverMode = true". 在mousedown上,我设置了一个标志“ inWhateverMode = true”。 The mousedown is important because it fires before the blur. 鼠标按下很重要,因为它会在模糊之前触发。
  2. In the blur handler, check to see if inWhateverMode. 在模糊处理程序中,检查是否处于inWhateverMode。
  3. On mouseup you cleanup any flags...complete the intended action. 在mouseup上,您清除所有标志...完成预期的操作。

This is handy when wanting to allow the user to click on a button, then move the cursor off the initial button, and then let go without negative impacts. 当希望允许用户单击按钮,然后将光标从初始按钮移开,然后放开而不会产生负面影响时,这非常方便。 Like inadvertently clicking a delete button. 就像无意间单击了删除按钮一样。

Updated Plunk: http://plnkr.co/edit/raJzMor9ci8dLgzXuQII?p=preview 更新的Plunk: http ://plnkr.co/edit/raJzMor9ci8dLgzXuQII?p=preview

ng-mousedown='startToShowItem(item)' ng-mouseup='show(item)'

Ok so I think this is what you're looking for: 好的,我想这就是您想要的:

http://plnkr.co/edit/r3SlOXg6VuTZDABPCAxq?p=preview http://plnkr.co/edit/r3SlOXg6VuTZDABPCAxq?p=preview

The function runs, and then the drop down disappears? 函数运行,然后下拉菜单消失了吗?

$scope.show = function(item) { $scope.hidden = true; $ scope.show = function(item){$ scope.hidden = true; alert(item) }; Alert(item)};

modified HTML: 修改后的HTML:

<input ng-model='search' />
<div ng-hide='hidden' style='border:1px solid black'>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM