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.
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.
ControlValueAccessor
Yes, you have to. That is the contract. Angular will call each method during the lifecycle of the custom form control.