我正在使用 Angular Material 步进器,它在 1 个组件中作为整个代码运行良好。但是步进器将用于大约 10 个不同的组件。有 7 种不同类型的表单,如输入、选择、名称、表格等。所以我想我会将 stepper 分成单独的组件,其中每个组件都是一个步骤。所以,我决定为表格制作一个模板app-step-form
<form [formGroup]="myForm">
<mat-vertical-stepper [linear]="isLinear" #stepper>
<ng-content></ng-content>
</mat-vertical-stepper>
</form>
而不是像这样创建不同的步骤app-table-select
<mat-step [stepControl]="tableControl">
<ng-template matStepLabel>Table </ng-template>
<mat-form-field appearance="fill">
<mat-label>Choose table</mat-label>
<mat-select [formControl]="tableControl">
<mat-option *ngFor="let table of tables" [value]="table.value">
{{table.viewValue}}
</mat-option>
</mat-select>
</mat-form-field>
<div>
<button mat-button matStepperNext>Next</button>
</div>
</mat-step>
而不是像这样的步骤插入组件
<app-step-from>
<app-table-select>
</app-table-select>
<app-name-input>
</app-name-input>
</app-step-from>
所以我会有表单模板在不同的组件中使用,而不是每次都一个一个地创建。
但我面临的问题是,如果我使用上面显示的步骤组件,它在app-step-form
. 对此进行小改动app-step-form
并提取<mat-step></mat-step>
<form [formGroup]="myForm">
<mat-vertical-stepper [linear]="isLinear" #stepper>
<mat-step>
<ng-content></ng-content>
</mat-step>
</mat-vertical-stepper>
</form>
然后像这样取出mat-step
并放入<ng-template>
<ng-template>
<ng-template matStepLabel>Table </ng-template>
<mat-form-field appearance="fill">
<mat-label>Choose table</mat-label>
<mat-select>
<mat-option *ngFor="let table of tables" [value]="table.value">
{{table.viewValue}}
</mat-option>
</mat-select>
</mat-form-field>
</ng-template>
使此步骤可见,app-step-form
但不能从中传递步骤组件标签,
<ng-template matStepLabel>Table </ng-template>
并且此标签不可见或@Input()
两者都不使用。
我花了 2 天的时间试图解决这个问题,寻找答案,但到目前为止还没有那么天真,也失去了如何克服这个问题以使我的步进器可重复使用的想法。
有谁知道如何使这个想法发挥作用?还是不可能按照我想象的方式进行?
还有任何想法如何使这些输入像 1 形式的一部分?子表单合并到 1 个表单还是什么?