每个步骤的角度材料步进组件

Ben*_*lly 10 angular-material angular

我有一个angular material linear stepper单独的angular component包含一个form需要的步骤validation

验证只是不起作用.我可以在不填写表格的情况下进入下一步.

为了说明我的意思,我在stackblitz上创建了一个浓缩版本.

要看的主要内容(我认为)是 create-profile.component.html

<mat-horizontal-stepper linear #stepper>
    <mat-step [stepControl]="frmStepOne">
        <ng-template matStepLabel>Step One Details</ng-template>
        <step-one-component></step-one-component>
    </mat-step>
    <mat-step [stepControl]="frmStepTwo">
        <ng-template matStepLabel>Step Two Details</ng-template>
        <step-two-component></step-two-component>
    </mat-step>
    <mat-step [stepControl]="frmStepThree">
        <ng-template matStepLabel>Step Three Details</ng-template>
        <step-three-component></step-three-component>
    </mat-step>
</mat-horizontal-stepper>
Run Code Online (Sandbox Code Playgroud)

每一个 step-X-component

这是stackblitz. https://stackblitz.com/edit/angular-vpoj5j

Spl*_*lex 20

问题出在您的CreateProfileComponent中:

@Component({
    selector: 'create-profile-component',
    templateUrl: './create-profile.component.html'
})
export class CreateProfileComponent {

    frmStepOne: FormGroup;
    frmStepTwo: FormGroup;
    frmStepThree: FormGroup;

    constructor(private fb: FormBuilder) { }


}
Run Code Online (Sandbox Code Playgroud)

CreateProfileComponent中定义的FormGroup与步进器组件之间没有任何关系.您尝试使用CreateProfileComponent扩展每个StepComponent,但是使用此方法,每个StepComponent都拥有自己的CreateProfileComponent实例,因此有自己的FormGroup声明.

要解决您的问题,您可以为html中的每个StepComponent声明模板变量(以#开头)并将formControl传递给[stepControl]:

<mat-horizontal-stepper linear #stepper>
    <mat-step [stepControl]="step1.frmStepOne">
        <ng-template matStepLabel>Step One Details</ng-template>
        <step-one-component #step1></step-one-component>
    </mat-step>
    <mat-step [stepControl]="step2.frmStepTwo">
        <ng-template matStepLabel>Step Two Details</ng-template>
        <step-two-component #step2></step-two-component>
    </mat-step>
    <mat-step [stepControl]="step3.frmStepThree">
        <ng-template matStepLabel>Step Three Details</ng-template>
        <step-three-component #step3></step-three-component>
    </mat-step>
</mat-horizontal-stepper>
Run Code Online (Sandbox Code Playgroud)

或者你保留你的html,并使用ViewChild()(我的首选方法).

@Component({
    selector: 'create-profile-component',
    templateUrl: './create-profile.component.html'
})

export class CreateProfileComponent {

    @ViewChild(StepOneComponent) stepOneComponent: StepOneComponent;
    @ViewChild(StepTwoComponent) stepTwoComponent: StepTwoComponent;
    @ViewChild(StepTwoComponent) stepThreeComponent: StepThreeComponent;

    get frmStepOne() {
       return this.stepOneComponent ? this.stepOneComponent.frmStepOn : null;
    }

    get frmStepTwo() {
       return this.stepTwoComponent ? this.stepTwoComponent.frmStepTwo : null;
    }

    get frmStepThree() {
       return this.stepThreeComponent ? this.stepThreeComponent.frmStepThree : null;
    }

}
Run Code Online (Sandbox Code Playgroud)

无论哪种方式,都不需要使用CreateProfileComponent扩展StepComponents,它没有任何意义.

@Component({
    selector: 'step-x-component',
    templateUrl: './step-x.component.html',
})
export class StepXComponent {

    public formStepX: FormGroup;

    constructor(private formBuilder: FormBuilder) {
    }

    ngOnInit() {
        this.frmStepX = this.formBuilder.group({
            name: ['', Validators.required]
        });

    }

}
Run Code Online (Sandbox Code Playgroud)

  • 只是一个注释:我得到了`ExpressionChangedAfterItHasBeenCheckedError:表达式在检查后发生了变化.上一个值:'undefined'.当前值:'[object Object]'.`错误并通过在构造函数中初始化各个步骤组件的形式来解决它. (5认同)
  • 感谢您的快速回复@SplitterAlex,我没有在模板中包含步骤组件引用(#step1,#step2),因为当您说“或者您保留 html 原样...”时,我有点困惑。我的错:) 现在工作完美,我的代码更容易维护。再次感谢。 (2认同)