问题描述
我想阻止 mat-option 被选中,因为点击它会打开一个对话框.只有在从对话框中选择某些内容时,才应该选择我的选项.如果对话框中没有选择任何内容,则 mat-option 不应更改为以前的值.
I want to prevent mat-option from being selected because clicking on it will open a dialog. Only when selecting something from the dialog, should my option be selected. If nothing was selected from the dialog, mat-option should not be changed from previous value.
<mat-select
[(ngModel)]="filter_defaultSelectedValue"
(change)="changeSelectedValue($event.value)">
<mat-option *ngFor="let filter of filters" [value]="filter">
<span *ngIf="filter.id != 'custom'; else content_dialog">
{{filter.label | i18n}}
</span>
<ng-template #content_dialog>
<dialog
[filterParams] = "filter.value">
</dialog>
</ng-template>
</mat-option>
</mat-select>
我的 mat-select 有以下选项:昨天"、今天"、明天"、自定义范围".例如,当我单击昨天"时,它只是被选中,但是当我单击自定义范围"时,会打开一个带有日历的对话框.如果我从日历中选择一个日期,对话框会关闭并且自定义范围"选项也被选中.当我关闭对话框而不从日历中选择任何内容时,自定义范围"选项将再次被选中.我不希望发生此选择,因为我没有从日历中选择任何内容.我该如何调节?
My mat-select has the following options: "yesterday", "today", "tomorrow", "custom range". For example when I click on "yesterday" it just gets selected, but when I click on "custom range" a dialog opens with a calendar. If I select a date from the calendar, dialog closes and the "custom range" option is selected as well. When I close the dialog without selecting anything from the calendar, "custom range" option gets selected again. I would not like this selection to happen since I didn't select anything from calendar. How can I condition this?
推荐答案
通过更改 ngModel "filter_defaultSelectedValue" 代码,我设法调整了这种特殊情况.如果对话框内没有选择任何内容,ngModel 将设置为某个先前设置的值:
I've managed to condition this special case by changing in code the ngModel "filter_defaultSelectedValue".If inside the dialog nothing was selected, ngModel is set to some previously set value:
this.filter_defaultSelectedValue = this.lastSelection;
如果选择了对话框内的某些内容,那么我让 mat-select 将我的filter_defaultSelectedValue"更改为所选值.
If something inside dialog was selected, then I let mat-select change my "filter_defaultSelectedValue" to that selected value.
这篇关于如果条件允许,则阻止垫选项选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!