Skip to main content

Posts

Showing posts from October 30, 2021

Angular - How to create custom form controls

Greetings! If you are working in an Angular project, you surely have created many UI components. We can easily use @Input, @Output to communicate data between parent to child and child to parent. However, we are unable to bind those values directly to angular forms. But, do we realy need to do it that way? Yes! we need, utilizing the form modules is more powerful way to get user input. Then, can't we use our 'normal' components to do the job? No! we can't. I was ashamed to know that I didn't know the existence of the ControlValueAccessor but better late than never ;) Complete code  custom-form-control What is ControlValueAccessor? When you are working with forms in Angular, whether it is template driven or reactive form, NgModel, formControl, formControlName, Angular creates FormControl to track the value, validations etc. Then Angular should have a way to communicate data between actual html element and the FormControl. Angular is smarter enough not to create one t