I am trying to encapsulate a component (for example called custom-input
) as a formControl element based on Angular material components to contain the following:
<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>
Now this component is working with the needed functionality but can't be bound to formGroup using the formControlName attribute like the normal input or matInput (like the below for example, which is bound directly to the formGroup without issues)
<input
matInput
formControlName="inputField123"
placeholder="{{ 'testInput' | translate }}"
required
/>
The question is how to enable the composed component above to be bound as a formControl element in the formGroup? what should I implement or expose to provide this functionality?
Should I use ControlValueAccessor or there is a better way that shall be used for Material input components?