0

这是正确的方法吗?官方文档只给了我们一个如何使用模板驱动表单而不是响应式表单的示例。

<mat-form-field>
    <mat-select placeholder="Select Toppings" formControlName="toppings" [compareWith]="compareFn" multiple>
        <mat-option *ngFor="let topping of (toppings$ | async)" [value]="topping.id">{{topping.name}}</mat-option>
    </mat-select>
</mat-form-field>

pizzaForm = this.formBuilder.group({
    id: ['1'],
    name: ['Foo'],
    topping: [
        [
            { id: 1, name: 'Foo'}
         , 
            { id: 2, name: 'Bar' }
        ]
    ],
})

compareFn(a: any, b: any): boolean {
    return a.id === b.id;
}
4

0 回答 0