检索选项时如何设置 mat-select 的默认值?
How to set default value of mat-select when options are retrieved?
<mat-form-field [ngClass]="classes">
<mat-select [placeholder]="placeholder" [(ngModel)]="selectValue" [multiple]="true" #multiSelect (change)="onChange()"
#itemSelect="ngModel">
<ngx-mat-select-search [formControl]="multiFilterCtrl"></ngx-mat-select-search>
<mat-option *ngFor="let option of filteredMulti | async" [value]="option.id">
{{option.name}}
</mat-option>
</mat-select>
</mat-form-field>
Angular material 多选如何设置来自 api?
的多选下拉列表的值
如果您尝试设置选定的值,您可以
// ...data coming from the api and saved to this.filteredMulti
this.filteredMulti.take(1).subscribe(() =>
// set your model
selectValue = apiData.selectValue
// setting the compareWith property to a comparison function
// triggers initializing the selection according to the initial value
// this needs to be done after the filteredMulti are loaded initially
// and after the mat-option elements are available
this.multiSelect.compareWith = (a: number, b: number) => a === b;
});
<mat-form-field [ngClass]="classes">
<mat-select [placeholder]="placeholder" [(ngModel)]="selectValue" [multiple]="true" #multiSelect (change)="onChange()"
#itemSelect="ngModel">
<ngx-mat-select-search [formControl]="multiFilterCtrl"></ngx-mat-select-search>
<mat-option *ngFor="let option of filteredMulti | async" [value]="option.id">
{{option.name}}
</mat-option>
</mat-select>
</mat-form-field>
Angular material 多选如何设置来自 api?
的多选下拉列表的值如果您尝试设置选定的值,您可以
// ...data coming from the api and saved to this.filteredMulti
this.filteredMulti.take(1).subscribe(() =>
// set your model
selectValue = apiData.selectValue
// setting the compareWith property to a comparison function
// triggers initializing the selection according to the initial value
// this needs to be done after the filteredMulti are loaded initially
// and after the mat-option elements are available
this.multiSelect.compareWith = (a: number, b: number) => a === b;
});