3

我有一个材料步进器,步进器的每个步骤都有表格。在那里,每个步骤都应该由与之关联的表单控制。

即使已经在 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>

现在,当我运行应用程序时,在控制台中,我看到以下内容。

ERROR 错误:ExpressionChangedAfterItHasBeenCheckedError:表达式在检查后已更改。以前的值:'stepControl:null'。当前值:'stepControl:[object Object]'。

正如我之前提到的,SO 中也有同样的讨论。例如,以下链接问题的答案和评论建议将表单初始化移至构造函数,而不是将其放在 onInit 中。我已经这样做了。

每个步骤的 Angular Material Stepper 组件

但是,我仍然收到错误消息。

这是我在 Stackblitz 上的项目 - https://stackblitz.com/github/vigamage/stepper-component-wise

有人可以建议如何摆脱这个问题吗?

谢谢..

4

3 回答 3

6

基本上,角度在已经null为这两个值设置值之后检测到变化。因此,您需要明确告诉 Angular 它应该运行更改检测。查看此演示代码

我已经删除了使用的逻辑,get因为直接从 HTML 调用函数是一种不好的做法。它会被多次调用。尝试将控制台日志放入您的代码中并查看。

我已经用ngAfterViewInitwithChangeDetectorRef来通知了。

export class AppComponent implements AfterViewInit {
  title = 'mat-stepper';

  form1: FormGroup;
  form2: FormGroup;
  @ViewChild('stepOne') stepOneComponent: FirstStepComponent;
  @ViewChild('stepTwo') stepTwoComponent: SecondStepComponent;

  constructor(private cdr :ChangeDetectorRef){}

  ngAfterViewInit(){
    this.form1 = this.stepOneComponent.frmStepOne;
    this.form2 = this.stepTwoComponent.frmStepTwo
    this.cdr.detectChanges();
  }

}

这将解决错误

于 2020-09-04T12:13:19.590 回答
3

只需将导致此错误的变量更改为 anObservablepipe使用delay(0) operator

这是一个工作解决方案的链接- 没有错误,也不需要使用变更检测。

这是一个例子

假设我在模板中使用了一个类型的变量,它会导致上面提到的错误namestringhtml

<div>{{ name }}</div> // BOOM ERROR why? 

因为起初name是一些东西,并且在角度已经检查过它之后它被改变了。

所以要解决这个问题..首先更改nameObservablelikename$

name$: Observable<string>; // name$ replaces name    

private myName$ = new BehaviorSubject<string>("");
myNameListener$: Observable<string> = this.myName$.asObservable();
myName(name: string) {
    this.myName$.next(name)
}

现在ngOnInit()只听那个 Observable

ngOnInit() {

    this.name$ = this.myNameListener$.pipe(
        delay(0)
    )

}

使用管道Observable在您的html模板中订阅它async

<div *ngIf=" (name$ | async) as name ">{{ name }}</div> // render your variable

现在将您的实际数据传递到您想要的任何地方。

this.myName(data);

这是一个工作解决方案的链接- 没有错误,也不需要使用变更检测。

祝你好运!

于 2020-09-04T11:43:50.103 回答
0

处理这个问题的一个非常简单的方法是在子模板中声明表单组,在父模板中,您可以引用它!这样做的主要好处是它非常安全,比所有其他选项更干净。

<mat-step [stepControl]="step1.step1Form">
  <app-step1 #step1></app-step1>
</mat-step>
于 2022-02-18T21:44:06.533 回答