javascript 什么是 AngularJS 中 jQuery 的模糊事件的替代?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17964027/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 10:14:45  来源:igfitidea点击:

What is the replacement of jQuery's blur event in AngularJS?

javascriptjqueryangularjsonblur

提问by Habibur Rahman

I have to close any opened component when clicking outside of that component using angularjs. Is there an angular directive for blur events? If not, how can I do that?

使用 angularjs 在该组件外部单击时,我必须关闭任何打开的组件。是否有用于模糊事件的角度指令?如果没有,我该怎么做?

回答by Jesus Rodriguez

If you don't want to use angular-ui's ui-event, you can also create a small directive until the next version of Angularis released.

如果你不想使用 angular-ui's ui-event,你也可以创建一个小指令,直到下一个版本Angular发布。

app.directive('ngBlur', function() {
  return function( scope, elem, attrs ) {
    elem.bind('blur', function() {
      scope.$apply(attrs.ngBlur);
    });
  };
});

Just put the directive where you need it:

只需将指令放在您需要的地方:

<input type="text" ng-model="foo" ng-blur="doFoo()" />

Basically what the directive does is to bind the blur event of the element (in our example the input) and then when the event is fired (we leave the input) angular will apply what is in the directive. So in our case, doFoo()will be fired if we leave the input.

基本上指令所做的是绑定元素的模糊事件(在我们的例子中是输入),然后当事件被触发时(我们离开输入)angular 将应用指令中的内容。所以在我们的例子中,doFoo()如果我们离开输入就会被解雇。

Plunker here: http://plunker.co/edit/J4ZEB6ppvkiIvdW9J2VU?p=preview

Plunker 在这里:http://plunker.co/edit/J4ZEB6ppvkiIvdW9J2VU?p=preview

回答by JQuery Guru

You can use Angular UI @ http://angular-ui.github.io/ui-utils/which provide Blurs, Focus, Double-Clicks event or Bind a callback to any event not natively supported by Angular Js

您可以使用 Angular UI @ http://angular-ui.github.io/ui-utils/,它提供模糊、焦点、双击事件或将回调绑定到 Angular Js 本身不支持的任何事件

Below is one of the example of blur event:

以下是模糊事件的示例之一:

<input ui-event="{ blur : 'blurCallback()' }">

<script>
$scope.blurCallback = function() {
alert('Goodbye');
};
</script>

回答by Walter Roman

As of Angular 1.2.24*, there is an ng-blurdirective.

从 Angular 1.2.24* 开始,有一个ng-blur指令。

// View
<input 
    type="text"
    placeholder="Hello World!"
    ng-model="foo.bar"
    ng-blur="onBlur($event)"
    >

// Controller
$scope.onBlur = function($event) {
    return $event;
}

* I don't know in which Angular version ng-blurwas introduced.

* 我不知道在哪个 Angular 版本ng-blur中引入。

回答by Thiago Mata

The native ng-blur "Doesn't work at all in latest unstable. :(" http://docs.angularjs.org/api/ng.directive:ngBlur

本机 ng-blur “在最新的不稳定版本中根本不起作用。:(” http://docs.angularjs.org/api/ng.directive:ngBlur

Yo can fix this following the steps of this post "There is no direct support for blur event in AngularJs. But we can add support for blur event by creating directive." http://coding-issues.blogspot.in/2013/10/angularjs-blur-directive.html

您可以按照“AngularJs 中不直接支持模糊事件。但我们可以通过创建指令添加对模糊事件的支持”这篇文章的步骤来解决这个问题。 http://coding-issues.blogspot.in/2013/10/angularjs-blur-directive.html

回答by tamakisquare

Directives supporting focus and blur events will be included in the next AngularJS release (reference), which should be out soon (my guess is within the next month). If you can't wait, like JQueryGuru suggested, you can use the uiEventdirective from the AngularUI project.

支持焦点和模糊事件的指令将包含在下一个 AngularJS 版本(参考)中,它应该很快就会发布(我的猜测是在下个月内)。如果您迫不及待,就像 JQueryGuru 建议的那样,您可以使用uiEvent来自 AngularUI 项目的指令。