【发布时间】:2016-04-27 08:53:38
【问题描述】:
如何在 Angular2 中检测 onBlur 事件? 我想用它
<input type="text">
谁能帮我理解如何使用它?
【问题讨论】:
标签: javascript angular onblur
如何在 Angular2 中检测 onBlur 事件? 我想用它
<input type="text">
谁能帮我理解如何使用它?
【问题讨论】:
标签: javascript angular onblur
另一种可能的选择
HTML 组件文件:
<input formControlName="myInputFieldName" (blur)="onBlurEvent($event)">
TypeScript 组件文件:
import { OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
export class MyEditComponent implements OnInit {
public myForm: FormGroup;
constructor(private readonly formBuilder: FormBuilder) { }
ngOnInit() {
this.myForm = this.formBuilder.group({
myInputFieldName: ['initial value', { validators: [Validators.required, Validators.maxLength(100), anotherValidator], updateOn: 'blur' }],
});
}
onBlurEvent(event) {
// implement here what you want
if (event.currentTarget && event.currentTarget.value && event.currentTarget.value !== '') { }
}
}
【讨论】:
你也可以使用(focusout)事件:
使用(eventName) 将事件绑定到DOM,基本上() 用于事件绑定。您也可以使用ngModel 为您的model 获得两种方式绑定。在ngModel 的帮助下,您可以在component 中操作model 变量值。
在 HTML 文件中执行此操作
<input type="text" [(ngModel)]="model" (focusout)="someMethodWithFocusOutEvent($event)">
在你的(组件).ts 文件中
export class AppComponent {
model: any;
constructor(){ }
/*
* This method will get called once we remove the focus from the above input box
*/
someMethodWithFocusOutEvent() {
console.log('Your method called');
// Do something here
}
}
【讨论】:
someMethodWithFocusOutEvent 中添加了一个警报,程序进入一个循环,因为警报使该字段失去焦点,有没有办法解决这个问题?
您可以在输入标签中直接使用 (blur) 事件。
<div>
<input [value]="" (blur)="result = $event.target.value" placeholder="Type Something">
{{result}}
</div>
你会在“result”中得到输出
【讨论】:
使用(eventName) 将事件绑定到DOM,基本上() 用于事件绑定。还可以使用ngModel 获得myModel 变量的两种方式绑定。
标记
<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 参数。
ctrl = new FormControl('', {
updateOn: 'blur', //default will be change
validators: [Validators.required]
});
【讨论】:
尝试使用 (focusout) 而不是 (blur)
【讨论】:
HTML
<input name="email" placeholder="Email" (blur)="$event.target.value=removeSpaces($event.target.value)" value="">
TS
removeSpaces(string) {
let splitStr = string.split(' ').join('');
return splitStr;
}
【讨论】:
这是 Github repo 上的建议答案:
// example without validators
const c = new FormControl('', { updateOn: 'blur' });
// example with validators
const c= new FormControl('', {
validators: Validators.required,
updateOn: 'blur'
});
Github : feat(forms): add updateOn blur option to FormControls
【讨论】:
/*for reich text editor */
public options: Object = {
charCounterCount: true,
height: 300,
inlineMode: false,
toolbarFixed: false,
fontFamilySelection: true,
fontSizeSelection: true,
paragraphFormatSelection: true,
events: {
'froalaEditor.blur': (e, editor) => { this.handleContentChange(editor.html.get()); }}
【讨论】: