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:

  1. 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

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -