Home > database >  What is the angular registerControl() method for
What is the angular registerControl() method for

Time:12-09

I saw that angular form had the following method registerControl(), but I couldn't really understand its usage.
Does somebody have a nice explanation and a use case for it?

I have the feeling I'm missing something that could be useful.

Thx for your help.

Edit

I already read the angular explanation but couldn't understand it

CodePudding user response:

https://github.com/angular/angular/blob/13.1.x/packages/forms/src/model.ts

Basically, we add another control to the form without invoking a check on value and validity, so there won't be many cases when you want to use it.

The only case which comes to my mind atm is when you want to register multiple controls without invoking value and validity check and invoke such check manually after you add all controls which you need so it would be triggered only once.

For example if I use addControl 3 times it would invoke updateValueAndValidity 3 times. But if I first registerControl 3 times and then manually invoke updateValueAndValidity or SetControl etc. on those 3 new controls then it would be invoked only once.

To summarize, it seems to be more about optimization. I didn't create any check for it but to be honest I would be surprised if that lead to a lot of saved time.

CodePudding user response:

registerControl() method - Registers a control with the group's list of controls.

registerControl(name: string, control: AbstractControl): AbstractControl Parameters name string The control name to register in the collection

control AbstractControl Provides the control for the given name

Returns AbstractControl.

This method does not update the value or validity of the control.

For example if I use addControl 3 times it would invoke updateValueAndValidity 3 times. But if I first registerControl 3 times and then manually invoke updateValueAndValidity or SetControl etc. on those 3 new controls then it would be invoked only once.

To summarize, it seems to be more about optimization.

  • Related