我想使用Angular Material在一段时间内禁用多个
示例:https://stackblitz.com/edit/angular-jqzx99
组件:
export class AppComponent {
@ViewChildren(MatOption) matSelectOptions: QueryList<MatOption>;
public isPaused = false;
public optionData = ['Foo', 'Bar', 'Baz'];
/**
* Disable MatSelect checkboxes for 2 seconds
*/
public disableCheckboxes(): void {
this.isPaused = true;
this.matSelectOptions.forEach(option => option.disabled = true);
setTimeout(() => {
this.isPaused = false;
this.matSelectOptions.forEach(option => option.disabled = false);
}, 2000);
}
}
模板:
<mat-form-field>
<mat-select multiple
[disabled]="isPaused"
(selectionChange)="disableCheckboxes()">
<mat-option *ngFor="let option of optionData">
{{option}}
</mat-option>
</mat-select>
<mat-placeholder>Example</mat-placeholder>
</mat-form-field>
<p id="loadingStatus">{{isPaused ? 'Pausing…' : ''}}</p>
答案 0 :(得分:1)
似乎以编程方式禁用MatOptions对象并不是一个好主意。
因此仅使用 isPaused 进行选择和选择即可正常工作
组件:
export class AppComponent {
@ViewChildren(MatOption) matSelectOptions: QueryList<MatOption>;
public isPaused = false;
public optionData = ['Foo', 'Bar', 'Baz'];
/**
* Disable MatSelect checkboxes for 2 seconds
*/
public disableCheckboxes(): void {
this.isPaused = true;
setTimeout(() => {
this.isPaused = false;
}, 2000);
}
}
模板:
<h3>Disabled MatSelect Checkbox Problem:</h3>
<hr>
<mat-form-field>
<mat-select multiple
[disabled]="isPaused"
(selectionChange)="disableCheckboxes()">
<mat-option *ngFor="let option of optionData" [disabled]="isPaused">
{{option}}
</mat-option>
</mat-select>
<mat-placeholder>Example</mat-placeholder>
</mat-form-field>
<p id="loadingStatus">{{isPaused ? 'Pausing…' : ''}}</p>