首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >检测mat-stepper验证失败的时间

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

Stack Overflow用户
提问于 2018-02-06 23:17:04
回答 3查看 6.5K关注 0票数 3

我们有一个线性的mat-horizontal-stepper,现在我们想要显示一个MatSnackBar,当用户试图继续一个忘记一个必填字段。

CdkStepper似乎静默地调用_anyControlsInvalidOrPending,并在任何输入无效时返回。

有谁知道如何检测这个事件吗?

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

EN

回答 3

Stack Overflow用户

发布于 2018-02-06 23:23:45

一个肮脏的解决方案是

代码语言:javascript
复制
  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,
        });
      }

    };
  }
票数 4
EN

Stack Overflow用户

发布于 2018-09-09 11:19:26

你需要添加一个“线性”属性

代码语言:javascript
复制
<mat-vertical-stepper linear>

如果表单无效,这将禁止导航到下一步。然后添加你的验证功能点击按钮,如果表单是无效的,然后你可以显示警报或快餐栏或任何你想要的。

票数 2
EN

Stack Overflow用户

发布于 2018-02-07 07:06:47

如果你能展示你的组件和包含mat-stepper的模板代码,那将会很有帮助。但是,从您的描述来看,我相信绑定到selectionChange事件会对您起作用。

在包含步进器的模板中:stepper-example.component.html

代码语言:javascript
复制
<mat-horizontal-stepper #stepper (selectionChange)="onStepChange($event)">
 <!-- your form -->
</mat-horizontal-stepper>

然后在您的组件中:stepper-example.component.ts

代码语言:javascript
复制
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
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48646275

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档