删除
ngModel
因为你在用
formControlName
.
我已经创建了一个演示
stackblitz
组件.ts
roleForm: FormGroup;
constructor(private fb: FormBuilder) {
this.roleForm = this.fb.group({
toppings: [null]
})
}
toppingList: string[] = ['Extra cheese', 'Mushroom', 'Onion', 'Pepperoni', 'Sausage', 'Tomato'];
selectAll() {
this.roleForm.get('toppings').patchValue(['Extra cheese', 'Mushroom', 'Onion', 'Pepperoni', 'Sausage', 'Tomato'])
}
deselectAll() {
this.roleForm.get('toppings').patchValue([])
}
组件.html
<form [formGroup]="roleForm">
<mat-form-field class="example-full-width">
<mat-select placeholder="Select Privileges" class="filter-select" formControlName="toppings" multiple>
<mat-option disabled="disabled" class="filter-option">
<button mat-raised-button class="mat-primary fill text-sm" (click)="selectAll()">
Select All
</button>
<button mat-raised-button class="mat-primary fill text-sm eta-margin-all" (click)="deselectAll()">
Deselect All
</button>
</mat-option>
<mat-option *ngFor="let privilege of toppingList" [value]="privilege">{{privilege}}</mat-option>
</mat-select>
</mat-form-field>
<p>Selected Value--{{roleForm.get('toppings').value}}</p>
</form>