我们有一个线性的mat-horizontal-stepper,现在我们想要显示一个MatSnackBar,当用户试图继续一个忘记一个必填字段。
CdkStepper似乎静默地调用_anyControlsInvalidOrPending,并在任何输入无效时返回。
有谁知道如何检测这个事件吗?
在调用stepper.next()、stepper.previous()和step.select()时必须检测到
发布于 2018-02-06 23:23:45
一个肮脏的解决方案是
ngAfterViewInit() {
// when clicking on the step header
this.stepper._steps.forEach((step) => {
this.addPriorValidyCheckToFunction(step, 'select');
});
// when calling previous and next function
this.addPriorValidyCheckToFunction(this.stepper, 'next');
this.addPriorValidyCheckToFunction(this.stepper, 'previous');
}
addPriorValidyCheckToFunction(object, functionName) {
// keep reference to AppComponent
let self = this;
// keep reference to original function
let oldFunction = object[functionName];
// replace original function
object[functionName] = function () {
// remember step before calling the function
let oldStep = self.stepper.selected;
// call the original function
oldFunction.call(object);
// if step did not change and form is invalid, show the message
if (oldStep == self.stepper.selected && !self.stepper.selected.stepControl.valid) {
self.snackBar.open("Fehler", "Bitte überprüfen Sie Ihre Eingaben", {
duration: 2000,
});
}
};
}发布于 2018-09-09 11:19:26
你需要添加一个“线性”属性
<mat-vertical-stepper linear>如果表单无效,这将禁止导航到下一步。然后添加你的验证功能点击按钮,如果表单是无效的,然后你可以显示警报或快餐栏或任何你想要的。
发布于 2018-02-07 07:06:47
如果你能展示你的组件和包含mat-stepper的模板代码,那将会很有帮助。但是,从您的描述来看,我相信绑定到selectionChange事件会对您起作用。
在包含步进器的模板中:stepper-example.component.html
<mat-horizontal-stepper #stepper (selectionChange)="onStepChange($event)">
<!-- your form -->
</mat-horizontal-stepper>然后在您的组件中:stepper-example.component.ts
import { Component, ViewChild } from '@angular/core';
@Component({
selector: 'stepper-example',
templateUrl: './stepper-example.component.html'
})
export class StepperExampleComponent {
@ViewChild('stepper') stepper;
public onStepChange(event): void{
//Some logic to validate your forms
}
}https://stackoverflow.com/questions/48646275
复制相似问题