【发布时间】:2020-09-04 11:18:39
【问题描述】:
我有一个材料步进器,步进器的每个步骤都有表格。在那里,每个步骤都应该由与之关联的表单控制。
即使在 SO 中已经提出了这个问题,这些问题的答案也不能解决我的问题。所以我在问。
父 HTML
<mat-horizontal-stepper linear #stepper>
<mat-step [stepControl]="frmStepOne">
<ng-template matStepLabel>Step One Details</ng-template>
<app-first-step #stepOne></app-first-step>
</mat-step>
<mat-step [stepControl]="frmStepTwo">
<ng-template matStepLabel>Step Two Details</ng-template>
<app-second-step #stepTwo></app-second-step>
</mat-step>
</mat-horizontal-stepper>
在我的父组件中,我有以下内容。
@ViewChild('stepOne') stepOneComponent: FirstStepComponent;
@ViewChild('stepTwo') stepTwoComponent: SecondStepComponent;
get frmStepOne() {
return this.stepOneComponent ? this.stepOneComponent.frmStepOne : null;
}
get frmStepTwo() {
return this.stepTwoComponent ? this.stepTwoComponent.frmStepTwo : null;
}
我的子类组件
frmStepOne: FormGroup;
constructor(private formBuilder: FormBuilder) {
this.frmStepOne = this.formBuilder.group({
name: ['', Validators.required]
});
}
子类 HTML
<mat-card>
<form [formGroup]="frmStepOne">
<mat-form-field>
<input matInput formControlName="name" matInput placeholder="Name" required>
</mat-form-field>
<mat-card-actions>
<button mat-raised-button matStepperNext class="nav-btn pull-right">Next</button>
</mat-card-actions>
</form>
</mat-card>
现在,当我运行应用程序时,在控制台中,我看到以下内容。
错误错误:ExpressionChangedAfterItHasBeenCheckedError:表达式 检查后有变化。以前的值:'stepControl:null'。 当前值:'stepControl: [object Object]'。
正如我之前提到的,SO 中也有相同的讨论。例如,以下链接问题的答案和 cmets 建议将表单初始化移动到构造函数,而不是将其放在 onInit 中。我已经这样做了。
Angular Material Stepper Component For Each Step
但是,我仍然收到错误消息。
这是我在 Stackblitz 上的项目 - https://stackblitz.com/github/vigamage/stepper-component-wise
有人可以建议如何解决这个问题吗?
谢谢你..
【问题讨论】:
-
还请修改您问题的标题。它与 Angular 材料及其步进器无关,更重要的是,我们需要感谢 Angular 在开发模式下向我们展示我们的代码有什么问题,因为在生产中你不会得到这个错误。
-
请找到我提供的解决方案。我修改了您的代码以使错误消失link to the solution 无需使用更改检测..您欠我一个更改检测:)
标签: angular typescript angular-material angular9 angular-material-stepper