79535430

Date: 2025-03-26 04:55:11
Score: 1
Natty:
Report link

What is the relationship?

NG_VALUE_ACCESSOR is the InjectionToken Angular uses to inject the value accessors. The ControlValueAccessor is the contract (aka interface) that every value accessor should implement.

Why do you need an Injection Token?

Injection tokens are required because the interfaces are gone once Angular's compiler translates the typescript code to JavaScript. There is no concept of an "interface" in JS.

There are different ways to define a provider. The most common one is when you simply define the class.

providers: [ MyClass ] // this is just a shorthand for { provide: MyClass, useClass: MyClass }

But when you want to provide an interface or a value, you need to use an injection token.

Do I need to implement ControlValueAccessor

Yes, you have to. That is the contract. Angular will call each method during the lifecycle of the custom form control.

Reasons:
  • Blacklisted phrase (0.5): I need
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Starts with a question (0.5): What is the
  • Low reputation (0.5):
Posted by: ingkevin