这是我的表单组。我在另一个内部使用了一个表单组:
this.shopGroup = this.fb.group({
_user: [''],
name: ['', Validators.compose([Validators.required, Validators.maxLength(60)])],
url_name: [''],
desc: ['', Validators.compose([Validators.required, Validators.maxLength(600)])],
photos: [''],
currency: ['Real'],
language: ['Português do Brasil'],
address: this.fb.group({
zipcode: ['', Validators.compose([Validators.required, Validators.pattern('[0-9]{5}[\-]?[0-9]{3}')])],
street: ['', Validators.compose([Validators.required, Validators.maxLength(70)])],
number: [null, Validators.compose([Validators.required, Validators.max(99999)])],
complement: ['', Validators.maxLength(30)],
district: ['', Validators.compose([Validators.required, Validators.maxLength(60)])],
state: ['', Validators.required],
city: ['', Validators.compose([Validators.required, Validators.maxLength(70)])]
}),
status: [true],
created_at: [new Date()],
updated_at: [new Date()]
});
这是模板:
<form [formGroup]="shopGroup">
<mat-horizontal-stepper [linear]="isLinear" #stepper>
// Im not sure how to set stepControl
<mat-step [stepControl]="?">
<ng-template matStepLabel>Store Creation</ng-template>
<mat-form-field>
<input matInput placeholder="Nome" formControlName="name">
</mat-form-field>
...
<div>
<button mat-button matStepperNext type="button">Next</button>
</div>
</mat-step>
// Im not sure how to set stepControl
<mat-step formGroupName="address" [stepControl]="?">
<ng-template matStepLabel>Configuração do Envio/Frete</ng-template>
<mat-form-field>
<input matInput placeholder="CEP" formControlName="zipcode">
</mat-form-field>
...
<div>
<button mat-button matStepperPrevious>Back</button>
<button mat-button matStepperNext>Next</button>
</div>
</mat-step>
<mat-step>
<ng-template matStepLabel>Done</ng-template>
You are now done.
<div>
<button mat-button matStepperPrevious>Back</button>
</div>
</mat-step>
</mat-horizontal-stepper>
</form>
这是单个表单的Angular Material
文档:
<form [formGroup]="formGroup">
<mat-horizontal-stepper formArrayName="formArray" linear>
<mat-step formGroupName="0" [stepControl]="formArray.get([0])">
...
<div>
<button mat-button matStepperNext type="button">Next</button>
</div>
</mat-step>
<mat-step formGroupName="1" [stepControl]="formArray.get([1])">
...
<div>
<button mat-button matStepperPrevious type="button">Back</button>
<button mat-button matStepperNext type="button">Next</button>
</div>
</mat-step>
...
</mat-horizontal-stepper>
</form>
我想在第一步使用shopGroup
,然后在第二步使用address
(在shopGroup内部分组)。最后,我要发送shopGroup
。我知道我需要在各个步骤之间设置type="button"
,并在最后设置type="submit"
,但是我不确定如何将[stepControl]
设置为从一个步骤移至另一步骤。如何使其在模板(html)上起作用?
答案 0 :(得分:0)
我在[stepControl]步骤中选择了所需的控件,而不是整个FormGroup,例如:<mat-step [stepControl]="shopGroup.controls['name']">
,它可以正常工作。
答案 1 :(得分:0)
您可以在official docs中阅读如何使用单一表格处理垫式踏步机
首先,您需要为表单组定义一个数组:
this.formGroup = this._formBuilder.group({
formArray: this._formBuilder.array([
this._formBuilder.group({
firstNameFormCtrl: ['', Validators.required],
lastNameFormCtrl: ['', Validators.required],
}),
this._formBuilder.group({
emailFormCtrl: ['', Validators.email]
}),
])
});
第二,您需要能够返回将在HTML模板上使用的formGroups数组:
get formArray(): AbstractControl | null {
return this.dictationForm.get('formArray');
}
然后,您应该将每个mat-step
与一个特定的组formArray
元素相关联:
<mat-step formGroupName="0" [stepControl]="formArray?.get([0])"></mat-step>
您可以找到一个有效的示例here