如何在Angular2上使用onBlur事件? [英] How to use onBlur event on Angular2?

查看:1653
本文介绍了如何在Angular2上使用onBlur事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Angular2中检测到onBlur事件?
我想与它一起使用

How do you detect an onBlur event in Angular2? I want to use it with

<input type="text">

任何人都可以帮我理解如何使用它吗?

Can anyone help me understand how to use it?

推荐答案

使用(eventName)将事件绑定到DOM,基本上()用于事件绑定。还可以使用 ngModel 来获得 myModel 变量的双向绑定。

Use (eventName) for while binding event to DOM, basically () is used for event binding. Also use ngModel to get two way binding for myModel variable.

加价

<input type="text" [(ngModel)]="myModel" (blur)="onBlurMethod()">

代码

export class AppComponent { 
  myModel: any;
  constructor(){
    this.myModel = '123';
  }
  onBlurMethod(){
   alert(this.myModel) 
  }
}

演示

备选(不可取)

<input type="text" #input (blur)="onBlurMethod($event.target.value)">

演示

用于模型驱动的表格开火验证 blur ,您可以传递 updateOn 参数。

For model driven form to fire validation on blur, you could pass updateOn parameter.

ctrl = new FormControl('', {
   debounce: 1000, 
   updateOn: 'blur', //default will be change
   validators: [Validators.required]
}); 

设计文档

这篇关于如何在Angular2上使用onBlur事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆