如何编程更改mat-button-toggle组状态
我正在尝试使用确认对话框更改MatbuttontoogleGroup状态,但是我找不到一种防止默认行为的方法(仅当我在CANCAL上更改为先前的状态时)。
html文件:
<mat-button-toggle-group [value]="value" (change)="onChange($event)">
<mat-button-toggle [value]="StatusType.Open">
Open
</mat-button-toggle>
<mat-button-toggle [value]="StatusType.Closed">
Closed
</mat-button-toggle>
</mat-button-toggle-group>
ts file:
@Input() value: StatusType;
@Output() statusChange = new EventEmitter<StatusType>();
onChange(event: MatButtonToggleChange): void {
const newValue = event.value;
if (newValue === StatusType.Open) {
this.statusChange.emit(value);
return;
}
const dialogRef = this.dialog.open(ConfirmationDialogComponent, {
data: {
title: 'Confirm status change',
message: 'Are you sure?',
}
});
dialogRef
.afterClosed()
.pipe(
untilDestroyed(this),
filter((result) => !!result)
)
.subscribe(() => {
this.statusChange.emit(newValue);
});
}
我实际需要的是在onChange
函数的开头添加event.preventDefault()
,但是matbuttontoggglechange 事件没有
destrestdefault()
方法,因为在幕后它实现了controlValueAccessor
。
I'm trying to change the MatButtonToogleGroup state with a confirmation dialog, but I can't find a way to prevent the default behavior (only if I change back to the previous state on Cancel).
HTML file:
<mat-button-toggle-group [value]="value" (change)="onChange($event)">
<mat-button-toggle [value]="StatusType.Open">
Open
</mat-button-toggle>
<mat-button-toggle [value]="StatusType.Closed">
Closed
</mat-button-toggle>
</mat-button-toggle-group>
TS file:
@Input() value: StatusType;
@Output() statusChange = new EventEmitter<StatusType>();
onChange(event: MatButtonToggleChange): void {
const newValue = event.value;
if (newValue === StatusType.Open) {
this.statusChange.emit(value);
return;
}
const dialogRef = this.dialog.open(ConfirmationDialogComponent, {
data: {
title: 'Confirm status change',
message: 'Are you sure?',
}
});
dialogRef
.afterClosed()
.pipe(
untilDestroyed(this),
filter((result) => !!result)
)
.subscribe(() => {
this.statusChange.emit(newValue);
});
}
And what I actually need is to add an event.preventDefault()
at the beginning of the onChange
function, but the MatButtonToggleChange
event does not have the preventDefault()
method because behind the scenes it implements the ControlValueAccessor
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试做的事情是使用
ngmodel
。您可以在组或所有切换按钮上添加ClickListener,然后打开对话框。用户确认对话框后,您可以设置模型。html文件
ts文件
这是一个stackblitz,其中一个示例很少:
https://stackblitz.com/edit/angular-layjkm?file=src%2fapp%2fbutton-toggle-appearance-example.ts
Something you could try to do is to use
ngModel
instead. You can add a clickListener on the group or all the toggle buttons, and then open the dialog. After the user confirms the dialog, you can set the model.HTML File
TS File
Here's a Stackblitz with a minimal example:
https://stackblitz.com/edit/angular-layjkm?file=src%2Fapp%2Fbutton-toggle-appearance-example.ts
这将需要重新分配到类型 matbuttontogglechange 的内部状态。在组中需要使用 ngmodel 来设置默认选择。您可以浏览已发出的活动的所有属性并使用它。
这类骇客应被视为最后的度假胜地,而更好的UX解决方案或组件选择将解决问题的根本原因
这是Stackblitz示例 https://stackblitz.com/edit/angular-xjn6ga-yxwytq?file=main.ts 。与Angular 13这样的最新版本也可以正常工作。
This would need a reassignment to the internal state of the event of type MatButtonToggleChange emitted by the toggle component. Usage of ngModel is needed on the group to set a default selection. You can go through all the properties of the event emitted and use it.
These kind of hacks should be considered as the last resort and better UX solutions or component choice will solve the root cause of problem better
Here is the stackblitz example https://stackblitz.com/edit/angular-xjn6ga-yxwytq?file=main.ts. Works fine with recent versions like Angular 13 too.