6

我正在我的一个组件中实现一个,如果我在属性存在mat-horizontal-stepper时从它移动,我试图让错误状态显示在一个步骤上,但这没有发生。[completed]false

我不确定使用该completed物业或类似的东西是否有一些限制;这就是我到目前为止所拥有的:

组件的 HTML

<mat-horizontal-stepper linear #auditStepper>
     <mat-step label="Choose a Company" [completed]="selectionClient.selected.length > 0">
     </mat-step>

     <mat-step label="Select a PPC Account" errorMessage="Select an Account" [completed]="selectionPPC.selected.length > 0">
     </mat-step>

     <mat-step label="Set Your Targets">  
     </mat-step>
</mat-horizontal-stepper>

组件的 TS

@Component({
  selector: 'app-new-audit',
  templateUrl: './new-audit.component.html',
  styleUrls: ['./new-audit.component.scss'],
  providers: [
    {
      provide:  STEPPER_GLOBAL_OPTIONS,
      useValue: { showError: true }
    }
  ]
})

在上面的代码中,我只提供了重要的东西;但是如果我正确地遵循了 Angular Material 文档,我需要做的是添加providers到组件(或我的主应用程序模块)中,就是这样?

因此,例如,如果我进入第 2 步但保持completed条件为假,然后移至另一个步骤,它应该会触发错误,因为该步骤不再处于活动状态但也未完成。

我只是想了解一切是如何工作的,因为我没有使用反应形式或任何实际用于此步进器的形式,因为我正在使用 MatTable 代替;我只需要用户从表中选择一行(通过 MatTable 的选择功能),如果选择数组有一个元素,那么我可以将该步骤视为“完成”并启用移动到下一步。

Stackblitz 演示 https://stackblitz.com/edit/angular-khyu8u

编辑:

FormGroup如果我对步骤使用 a和属性,则错误状态可以正常工作[stepControl],但我需要它而不需要表单。

4

1 回答 1

10

有专门的方法描述了显示错误所需的内容:

private _getDefaultIndicatorLogic(step: CdkStep, isCurrentStep: boolean): StepState {
  if (step._showError && step.hasError && !isCurrentStep) {
    return STEP_STATE.ERROR;
  }
  • step._showError来自STEPPER_GLOBAL_OPTIONS您在提供者中定义的
  • step.hasError包括最有趣的部分

以下是所有定义:

@Input()
get hasError(): boolean {
  return this._customError == null ? this._getDefaultError() : this._customError;
}

set hasError(value: boolean) {
  this._customError = coerceBooleanProperty(value);
}

private _getDefaultError() {
  return this.stepControl && this.stepControl.invalid && this.interacted;
}

如您所见,hasError 返回trueif

1)我们有stepControl无效的状态和当前步骤交互

2) 我们传递hasError返回 true 的 Input 道具

  • !isCurrentStep表示只有在您执行其他步骤时才会显示错误

因此,您可以hasError使用自定义逻辑将属性传递给步骤,例如:

<mat-step ... #step [hasError]="step.interacted" 

分叉的 Stackblitz

于 2019-09-01T19:28:49.167 回答