Skip to content

Reusable Form Components

Reusable form components encapsulate common form patterns and can be used with template-driven and reactive forms via ControlValueAccessor.

@Component({
providers: [{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => StarRatingComponent),
multi: true
}]
})
export class StarRatingComponent implements ControlValueAccessor {
writeValue(value: any) { /* ... */ }
registerOnChange(fn: any) { /* ... */ }
registerOnTouched(fn: any) { /* ... */ }
setDisabledState(isDisabled: boolean) { /* ... */ }
}

ControlValueAccessor makes any component compatible with Angular forms.”