Why registerOnValidatorChange in Validator instances overwrites current onChange handler?
Why registerOnValidatorChange in Validator instances overwrites current onChange handler?
There is Validator interface in Angular. There are a lot of predefined validators in Angular (like RequiredValidators and stuff).
Why registerOnValidatorChange in Validator instances overwrites current onChange handler?
Here is example of the code: https://github.com/angular/angular/blob/4.4.6/packages/forms/src/directives/validators.ts#L95
So what was the motivation?
The problem is: I am trying to watch changing required state of some control. I used registerOnValidatorChange
for that. After that I tried it on another control (inside ng-form
element) and I realised, that my watcher is not working. It is so, since ngForm
redefined onChange handler by calling registerOnValidatorChange
method again.
registerOnValidatorChange
ng-form
ngForm
registerOnValidatorChange
1 Answer
1
You have to notify the form when a validator's internal state has changed.
When you create a validator as a directive. That directive can have @Input()
bindings that provide data for calculating the validator result.
@Input()
If that @Input()
binding is changed, then the form does not know that the validator has been modified.
@Input()
So registerOnValidatorChange
is called to pass a callback function to the directive. You keep a reference to that callback and call it when ever the directive is changed. This tells the form to check the validator again.
registerOnValidatorChange
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
@AngularInDepth.com any ideas?
– Sharikov Vladislav
Mar 28 '18 at 16:08