angular - 如何为 matStepperNext 创建条件?
问题描述
我有一个 3 步材料步进器,第一步是检查数据是否在数据库中。如果它在,将弹出一个警告窗口,但该步骤将移至下一个。我试图解决 stepper.previous() 解决方案,但同样的方式它会自动跳转到下一步,然后才返回第一个。我希望如果弹出警报窗口,它不会进入下一步,而是留在那里。
这是第一步(我不复制其余部分。)
<mat-vertical-stepper [linear]="isLinear" #stepper>
<mat-step [stepControl]="nameFormGroup">
<mat-card>
<mat-card-content>
<form [formGroup]="nameFormGroup">
<ng-template matStepLabel>
Fill out your organization name.
</ng-template>
<mat-form-field >
<input
type="text"
matInput
placeholder="Name of organization"
formControlName="name"
required
>
<mat-error *ngIf="nameFormGroup.invalid">
Name is required.
</mat-error>
</mat-form-field>
<div>
<button [disabled]="nameFormGroup.invalid"
(click)="checkOrganization()"
mat-raised-button
matStepperNext
color="primary">Next</button>
</div>
</form>
</mat-card-content>
</mat-card>
</mat-step>
</mat-vertical-stepper>
.ts:
@ViewChild('stepper', { static: false }) stepper: MatStepper;
checkOrganization() {
this.orgService.checkOrganization(this.nameFormGroup.value.name, this.stepper);
}
服务:
checkOrg: boolean;
checkOrganization(name: string, stepper?: MatStepper) {
this.db.collection('organization').doc(name).get().subscribe(doc => {
if(doc.exists) {
this.checkOrg = false;
stepper.previous();
this.presentAlert();
}else {
this.checkOrg = true;
}
});
}
那么如果弹出alert窗口,stepper没有进入下一步怎么解决呢?
解决方案
删除matStepperNext
按钮并使用手动导航到下一步this.stepper.next()
<button [disabled]="nameFormGroup.invalid"
(click)="checkOrganization()"
mat-raised-button
color="primary">Next</button>
在您的 ts 文件验证功能中
checkOrg: boolean;
checkOrganization(name: string, stepper?: MatStepper) {
this.db.collection('organization').doc(name).get().subscribe(doc => {
if(doc.exists) {
this.checkOrg = false;
stepper.previous();
this.presentAlert();
}else {
this.checkOrg = true;
stepper.next();
}
});
}
这里是 stackblitz 演示:https ://stackblitz.com/edit/angular-pq24e5-tqqgrg?file=src/app/stepper-overview-example.ts
推荐阅读
- graphviz - 有没有办法使用graphviz中的fdp布局在同一集群中的节点之间与不同集群中的节点之间具有不同的边样条?
- excel - 双循环
- c# - 属性更改不会从任务内部更新 UI
- deep-learning - 用于二进制分类的最终线性层中的输出数量
- django - Django中的一对一关系
- python - 无法在ubuntu环境下安装autotest
- python - 如何使用数据结构找到具有给定眼睛颜色的人?
- python - PySpark:如果单行则将行合并到其父行,否则添加行是子行
- c++ - 指向 C++ 中未定义函数和参数的指针
- node.js - 使用 vue 和 axios 将图像发送到后端