angular - mat-stepper 组件中的每一步
问题描述
我想实现一个垫子步进器,每一步都在不同的组件中,我的问题是如果步骤未完成,防止单击下一步按钮:
parent.html:
<mat-horizontal-stepper>
<mat-step [stepControl]="stepOneForm [completed]="stepOneForm?.valid">
<ng-template matStepLabel>Step1</ng-template>
<child-step-1
[stepOne]="stepOneForm"
(stepOneEmitter)="updateStepOne($event)">
</child-step-1 >
</mat-step>
<mat-step [stepControl]="stepTwoForm [completed]="stepTwoForm?.valid">
<ng-template matStepLabel>Step2/ng-template>
<child-step-2
[stepTwo]="stepTwoForm"
(stepTwoEmitter)="updateStepTwo($event)">
</child-step-2 >
</mat-step>
</mat-horizontal-stepper>
parent.ts:
stepOneForm: FormGroup;
stepTwoForm: FormGroup;
//////////
updateStepOne(step: FormGroup) {
this.stepOneForm = step;
}
updateStepTwo(step: FormGroup) {
this.stepTwoForm = step;
}
child-1.html
<form [formGroup]="stepOne" (change)="updateStepOne()">
<mat-form-field>
<input matInput required type="text" formControlName="title"/>
</mat-form-field>
<button mat-button matStepperNext><mat-icon>arrow_forward</mat-icon></button>
</form>
child-1.ts
this.stepOne = this.fb.group({
title: ['Default title', [
Validators.required
]]
});
updateStepOne() {
this.stepOneEmitter.emit(this.stepOne);
}
一切正常,除了 stepControl,即使表单无效,也可以进入下一步。
我的错误在哪里?
解决方案
您正在使用[stepControl]
和[completed]
同时。在这种情况下stepControl
,将被解释为更重要[completed]
。如官方文档中所述:
或者,如果您不想使用 Angular 表单,您可以将完成的属性传递给每个步骤,这将不允许用户继续,直到它变为真。请注意,如果同时设置了 completed 和 stepControl,则 stepControl 将优先。
这是链接> https://material.angular.io/components/stepper/overview
推荐阅读
- ios - (iOS Cordova) 从远程访问本地文件 - WKWebview
- javascript - ipfs.cat() 返回承诺 {
即使我按照这个例子 - html - 为什么我的左填充在这个内联 css 中丢失了?
- python - Python上皇后问题的错误输出
- python - 我正在尝试在以下代码段中使用 argpase 模块
- mongodb - 当我们加载应用程序和 F4 Popup 并单击 Next 按钮时,我们会从 Mongodb Server 得到延迟响应
- google-chrome - Chrome 版本 90 - 输入类型时间
- java - 如何在 SpringBoot 中连接类和对象
- flutter - Flutter Hive - 管理已保存数据的最佳方式
- python - Python - 列表中的字符串到numpy数组