javascript - Set timeout to Angular Reactive From Validation message ngIF -
hi i'm using angular (4) , have custom validator , display via ngif
invalid(fieldname: string) { return ( this.form.get(fieldname).haserror('pattern') && this.form.get(fieldname).dirty && !this.required(fieldname) ); }
i want delay return
response user has time type couple of characters before error (if any) message displayed. name
value of field in question. i.e. firstname
html
<div *ngif="invalid(fieldname)" class="error-message"> invalid </div>
ideally used on email field @ https://twitter.com/signup don't want use .touched
thanks in advance.
you can delay response using rxjs operator debouncetime on form control's valuechanges observable:
- import 'rxjs/add/operator/debouncetime'; in component.
2.in subscribe can set component level variable holds result of invalid function , in html need bind variable div toggle error message.
this.form.get("name").valuechanges.debouncetime(1000) .subscribe(data => this.error = this.invalid(data));
<div *ngif="iserror" class="error-message"> invalid </div>
note :- can subscribe valuechanges on form element level write common solution form fields. refer onvaluechanged function in angular doc details.
Comments
Post a Comment