Given the below form I am able to manage the data and validation state of my component automatically as driven by the underlying Angular and HTML implementation
However, as soon as I move my input into a different component I am usually forced to do lots of weird things that steer me further away from a more simplified HTML-directed form. For the sake of simplicity and maintainability however I would like to be able to define a component that is able to take advantage of the Angular/HTML form integration while also providing me with the benefits of a component-based form input
Implementing a ControlValueAccessor
In order to do this, I can move the input into a new component provided that the component implements the ControlValueAccessor interface and the Validation interface if I would also like to use Angular Validation with my component
The basic implementation of a component that meets this requirement can be seen below:
Now that we have this component, we can simply swap out the use of input to use our new component in our example form:
The above provides a basis for any input component we want. It is also possible to define the above as a base class that can then be extended by other components to provide a more specific implementation such as working with a generic value to be a bit more type safe or to allow more specific variations or any additional styling to be contained to a specific component