My goal is to create a custom component (let's call it custom-input
) that acts as a formControl element using Angular material components. It should include the following structure:
<mat-form-field >
<input
matInput
[formControl]="inputField"
formControlName="{{ name }}"
name="{{ name }}"
maxlength="{{ maxlength }}"
placeholder="{{ name | translate }}"
required
/>
<mat-error *ngIf="inputField.errors">{{
this.validationService.getErrorMsg(inputField.errors)
}}</mat-error>
</mat-form-field>
The current setup works as intended but I'm facing an issue where my custom component cannot be directly bound to a formGroup using the formControlName attribute, unlike a normal input or matInput. The following code, for instance, binds successfully without any problems:
<input
matInput
formControlName="inputField123"
placeholder="{{ 'testInput' | translate }}"
required
/>
My question is how can I enable my composed component above to be bound as a formControl element within a formGroup? What steps should I take or what should I expose in order to make this functionality possible?
Should I use ControlValueAccessor or is there a different approach that would be more suitable for Material input components?