angular - 检测 mat-stepper 验证失败的时间

标签 angular validation angular-material2 angular-material-stepper

我们有一个线性 mat-horizontal-stepper现在我们要显示一个 MatSnackBar当用户尝试进行时忘记了必填字段。
CdkStepper好像打电话_anyControlsInvalidOrPending如果任何输入无效,则静默并返回。

有人知道如何检测此事件吗?

调用stepper.next()时必须检测到, stepper.previous()step.select()

最佳答案

一个肮脏的解决方案是

  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,
        });
      }

    };
  }

关于angular - 检测 mat-stepper 验证失败的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48646275/

相关文章:

带路由器的 Angular2 Material 设计 mdtabs

angular - 如何删除必填字段中的星号并替换 Angular 2+ 和 Angular Material 2 中占位符前的自定义星号

angular - 如何使用 Angular Material 2 制作侧面导航组件

angular - 如何在没有表单控件但使用 matInput 的情况下显示 mat-error 消息?

html - 为什么即使导入依赖项后 mat-form-field 也不起作用?

angular - ASP.NET Core Web 应用程序 : upgrade to Angular 9 - error es2015 as esm2015

java - JSF 2 - f :selectItems with a Date keyed Map

angular - 使用 Typescript 获取 Assets 文件夹中的文件名列表

ruby-on-rails - 用于验证仅包含字符(非特殊字符)、空格和数字的字符串的正则表达式

ruby-on-rails - Rails,验证电子邮件排除