角形垫选择以表格

时间:2020-01-09 12:09:48

标签: html angular typescript angular-material

有一个带有嵌套对象的数据模型:

export interface Model {
  someField1:string;
  someField2:string;
  someObject: Object;
}

export interface Object {
  someField1: string;
  someField2: string;
  someField3: string;
}

此模型有一种形式:

formBuilder.group({
  someField1: null,
  someField2: null,
  someObject: formBuilder.group({
    someField1: null,
    someField2: null,
    someField3: null
})

此对象有一个角度分量:

  <mat-form-field>
    <mat-select placeholder="Some Object" formGroupName="someObject">
      <mat-option *ngFor="let object of someObjectes" [value]="object">
        {{ object.someField1 }}
      </mat-option>
    </mat-select>
  </mat-form-field>

我接受来自服务器的一系列对象,然后选择用户需要的对象。必须将选择绑定到表单,并且如果接收的模型已经具有有关对象的数据,则应将其显示为预选项目。

1 个答案:

答案 0 :(得分:1)

Angular Material mat-select提供了一个compareWith API,您可以向其传递映射功能以设置默认值。

这是正在工作的Stackblitz example