[英]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.
就我而言,这是一种取消编辑按钮。
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.