![](/img/trans.png)
[英]In Angular2 ngModel value not updating on onBlur event of custom directive
[英]How to use onBlur event on Angular2?
如何在 Angular2 中檢測到 onBlur 事件? 我想用它
<input type="text">
誰能幫我理解如何使用它?
將事件綁定到 DOM 時使用(eventName)
,基本上()
用於事件綁定。 此外,使用ngModel
為myModel
變量獲取雙向綁定。
標記
<input type="text" [(ngModel)]="myModel" (blur)="onBlurMethod()">
代碼
export class AppComponent {
myModel: any;
constructor(){
this.myModel = '123';
}
onBlurMethod(){
alert(this.myModel)
}
}
備選方案 1
<input type="text" [ngModel]="myModel" (ngModelChange)="myModel=$event">
備選方案 2 (不優選)
<input type="text" #input (blur)="onBlurMethod($event.target.value)">
對於要在blur
上觸發驗證的模型驅動表單,您可以傳遞updateOn
參數。
ctrl = new FormControl('', {
updateOn: 'blur', //default will be change
validators: [Validators.required]
});
您還可以使用(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
}
}
您可以在輸入標簽中直接使用(模糊)事件。
<div>
<input [value]="" (blur)="result = $event.target.value" placeholder="Type Something">
{{result}}
</div>
你會在“結果”中得到輸出
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;
}
/*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()); }}
這是 Github repo 上的建議答案:
// example without validators
const c = new FormControl('', { updateOn: 'blur' });
// example with validators
const c= new FormControl('', {
validators: Validators.required,
updateOn: 'blur'
});
嘗試使用 (focusout) 而不是 (blur)
另一種可能的選擇
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 !== '') { }
}
}
我最終在 Angular 14 中做了這樣的事情
<form name="someForm" #f="ngForm" (ngSubmit)="onSubmit(f)"> ...
<input
matInput
type="email"
name="email"
autocomplete="email"
placeholder="EMAIL"
[ngModel]="payload.email"
#email="ngModel"
(blur)="checkEmailBlur(f)"
required
email
tabindex="1"
autofocus
/>
然后... in.ts
checkEmailBlur(f: NgForm) {
const email = f.value.email;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.