I am dealing with an input field that has a customValidator called fooValidator
. This custom validator checks if the input matches a specific regular expression:
<form #contratForm="ngForm">
<input type="text"
class="validate"
[(ngModel)]="foo"
name="foo"
ngControl="foo"
fooValidator
(ngModelChange)="blah($event)"
required
/>
</form>
Within my component, I have the following code:
blah(event) {
if(this.contratForm.controls.foo.valid){
console.log("Valid")
}
}
I am facing an issue where "Valid" is not being displayed in the console. This happens because the ngModelChange event is triggered before the validation. One workaround I found is to wrap my blah
function in a setTimeout
, which makes "Valid" appear. Is there a cleaner way to achieve this without using a hacky setTimeout
?