0

Let's say I am creating a custom checkbox component, that has a checkbox inside of it. I just want to use the component, just so I can style it on certain conditions. As far as I know, I need to implement ControlValueAccessor interface to be able to use my component as a form component. Is there a way to provide the element inside as the ControlValueAccessor, rather than writing the whole boilerplate.

Something like this,

@Component({
  selector: 'app-custom-checkbox',
  templateUrl: './custom-checkbox.component.html',
  styleUrls: ['./custom-checkbox.component.scss'],
  providers: [{
    provide: NG_VALUE_ACCESSOR, 
    useExisting: "" // Use the checkbox in the template
  }]
})
export class CustomCheckboxComponent {}

ertucode
  • 560
  • 2
  • 13
  • A custom form control should be some more than "a checkbox with label or with style". We need ask us if it's neccesary or a simple component do the job. You can use, e.g. viewProviders, -see this [SO](https://stackoverflow.com/questions/73455294/how-do-i-get-access-to-from-control-from-another-component-in-angular/73457780#73457780) – Eliseo Oct 31 '22 at 15:45
  • I knew about that "solution", but It's not exactly what I want. And,I think it's worth to have a component since I'll be using it multiple places. Also, I was curious about this too. – ertucode Oct 31 '22 at 20:13

0 Answers0