Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is forwardRef mandatory while creating NG_VALUE_ACCESSOR

I have a custom input component which implements ControlValueAccessor with providers declared as follows. It seems to work fine. In all the tutorials I could find on the internet, forwardRef was being used extensively whenever NG_VALUE_ACCESSOR is provided

Is it safe to send the following code to production ?

providers: [{
  provide: NG_VALUE_ACCESSOR,
  useExisting: CustomInputComponent,  //Notice I omitted forwardRef here and it works fine
  multi: true
}]
like image 858
Surya KLSV Avatar asked May 27 '20 16:05

Surya KLSV


1 Answers

forwardRef enables Angular to inject a dependency before it is defined. In this case, if this providers array is defined inside the @Component decorator for a custom input component as you mentioned, it will work since decorators are applied after the class is defined.

Basically, you can remove forwardRef as long as you are only using it to reference a component inside the decorator for that same component.

See here for in depth reading

like image 99
Paul Avatar answered Dec 10 '22 03:12

Paul