0

StackBlitz(可运行示例)

组件的 Angular HTML 模板

<form [formGroup]="myForm">
  <mat-form-field>
    <input matInput placeholder="name" formControlName="name">
  </mat-form-field>

  <mat-form-field>
    <mat-list formControlName="foldersList">
      <mat-list-item *ngFor="let folder of folders"><h4 mat-line>{{folder.name}}</h4></mat-list-item>
    </mat-list>
  </mat-form-field>
</form>

Angular TypeScript 组件

export class MyComponent {
  folders = [ { name: 'Photos', updated: new Date('1/1/16') } ];

  myForm: FormGroup = this.fb.group({
    name: ['', Validators.required],
    foldersList: 
           new FormControl([]),
           // new FormArray([]),
           // ['', Validators. Validators.required],
           // this.fb.array([], [Validators.required]),
  });

  constructor(private fb: FormBuilder) {}
}

错误

Error: No value accessor for form control with name: 'foldersList'
Error: mat-form-field must contain a MatFormFieldControl.
4

1 回答 1

4

和组件不实现mat-list接口。这就是为什么它们不能在.mat-selection-listMatFormFieldControlmat-form-field

这是上述示例的工作分支:https ://stackblitz.com/edit/angular-xijtrv-yzpsmj

只是替换mat-listmat-selection-list删除了mat-form-field包裹它的东西。

于 2019-11-29T06:44:44.870 回答