javascript - How to wrap angular material form into a component & expose them to FormGroup as a formControl element - St

I am trying to encapsulate a ponent (for example called custom-input) as a formControl element based on

I am trying to encapsulate a ponent (for example called custom-input) as a formControl element based on Angular material ponents 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 ponent 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 posed ponent 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 ponents?

I am trying to encapsulate a ponent (for example called custom-input) as a formControl element based on Angular material ponents 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 ponent 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 posed ponent 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 ponents?

Share Improve this question asked Feb 25, 2019 at 13:29 Ahmed ElkoussyAhmed Elkoussy 8,59812 gold badges67 silver badges92 bronze badges 1
  • 2 "Should I use ControlValueAccessor". Yes. – Roberto Zvjerković Commented Feb 25, 2019 at 14:07
Add a ment  | 

1 Answer 1

Reset to default 8

After searching around for long time, mainly because the ControlValueAccessor was advised for the primitive html parts (like input or so, not for material ponents, which I thought might have some easier wrapping technique)

I finally found out that we can implement ControlValueAccessor as advised, or since this is an input field, we can use DefaultValueAccessor like this

@Component({
  selector: 'app-test-input',
  templateUrl: './test-input.ponent.html',
  styleUrls: ['./test-input.ponent.css'],
  providers: [
    {
      provide: NG_VALUE_ACCESSOR,
      useExisting: forwardRef(() => testInputComponent),
      multi: true
    }
  ]
})
export class testInputComponent implements ControlValueAccessor {
  @Input() name = '';
  @Input() maxlength = 250;
  @Input() width = 30;

  @ViewChild(DefaultValueAccessor) valueAccessor: DefaultValueAccessor;

  writeValue(obj: any) {
    this.valueAccessor.writeValue(obj);
  }

  registerOnChange(fn: any) {
    this.valueAccessor.registerOnChange(fn);
  }

  registerOnTouched(fn: any) {
    this.valueAccessor.registerOnTouched(fn);
  }

  setDisabledState(isDisabled: boolean) {
    this.valueAccessor.setDisabledState(isDisabled);
  }

  constructor(public dataService: DataService) {
  }

}

The html part is like this:

<mat-form-field fxFlex="100" >
  <input
    matInput
    formControlName="{{ name }}"
    maxlength="{{ maxlength }}"
    placeholder="{{ name | translate }}"
    required
  />
  <mat-error *ngIf="matInputSelector.errors">{{
    this.dataService.getErrorMsg(matInputSelector.errors)
  }}</mat-error>
</mat-form-field>

Note

if I use formControlName="{{ name }}" I can bind the control to the formGroup, however, I can't use the formControl attribute to bind to the error part of the ponent

i.e: I can either use:

1- formControl (can control the error part)

OR

2- use formControlName (binded to the outer formGroup, but can't control the error part)

So if someone has a better answer, please post it to help everyone.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744707104a4589119.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信