简体   繁体   中英

How to trigger Angular FormControl validation without triggering valueChanges?

I'm curious if anyone has found a way to trigger validation without using updateValueAndValidity() (which also triggers valueChanges on a FormControl according to the docs which isn't always a desired behavior) or markAsTouched() (which is simply incorrect as we didn't touch the control).

A workaround that comes to mind would be to extract validators logic in a way that it could be executed outside of FormControl and then do setErrors() based on the results, but maybe there's a better way of doing that?

See official doc:

https://angular.io/api/forms/AbstractControl#updateValueAndValidity

emitEvent: When true or not supplied (the default), both the statusChanges and valueChanges observables emit events with the latest status and value when the control is updated. When false, no events are emitted.

Edit: So you can validate without emitting an event.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM