[英]Prevent mat-option selection if condition
I want to prevent mat-option from being selected because clicking on it will open a dialog. 我想防止选择mat-option,因为单击它会打开一个对话框。 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-option更改为先前的值。
<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?
我该如何调节?
I've managed to condition this special case by changing in code the ngModel "filter_defaultSelectedValue". 我设法通过更改代码中的ngModel“ filter_defaultSelectedValue”来处理这种特殊情况。 If inside the dialog nothing was selected, ngModel is set to some previously set value:
如果在对话框中未选择任何内容,则将ngModel设置为某个先前设置的值:
this.filter_defaultSelectedValue = this.lastSelection;
If something inside dialog was selected, then I let mat-select change my "filter_defaultSelectedValue" to that selected value. 如果选择了对话框中的某些内容,则可以让mat-select将“ filter_defaultSelectedValue”更改为该选定值。
Thanks, this helped me. 谢谢,这对我有帮助。 I had a very similar use case and I'm using reactive forms.
我有一个非常相似的用例,并且正在使用反应形式。 In my app if a user selects a particular value from the mat-select a special warning popup is shown with Accept, Cancel buttons.
在我的应用程序中,如果用户从垫子中选择特定值,请选择“接受”,“取消”按钮,显示一个特殊的警告弹出窗口。 If the user accepts, the particular value will stay set.
如果用户接受,则特定值将保持不变。 But, if the user chooses to cancel, the previous value should be set.
但是,如果用户选择取消,则应设置先前的值。 As the
(valueChange)
event on mat-select fires before the model [(ngModel)]
is updated I was able to store the previous value in a component property called previousOption
. 由于在更新模型
[(ngModel)]
之前触发了mat-select上的(valueChange)
事件,因此我能够将先前的值存储在名为previousOption
的组件属性中。 I then databound another property called currentOption
to the [(ngModel)]
and with event binding (valueChanges)
set the previous option to the current option. 然后,我将另一个名为
currentOption
属性绑定到[(ngModel)]
并使用事件绑定(valueChanges)
将前一个选项设置为当前选项。 You can then hookup your (selectionChange)
to do whathever. 然后,您可以连接
(selectionChange)
以执行任何操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.