如果有条件则防止选择垫选项 [英] Prevent mat-option selection if condition
问题描述
我想防止选择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>
我的选择垫具有以下选项:昨天",今天",明天",自定义范围".例如,当我单击昨天"时,它刚刚被选中,但是当我单击自定义范围"时,将打开一个带有日历的对话框.如果我从日历中选择一个日期,则对话框将关闭,同时也会选择自定义范围"选项.当我关闭对话框而不从日历中选择任何内容时,自定义范围"选项将再次被选中.我不希望发生这种选择,因为我没有从日历中选择任何内容.我该如何调节?
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.
这篇关于如果有条件则防止选择垫选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!