在Angular中,是否可以使用线性步进器,其中各个步骤是单独的组件?例如:
<mat-horizontal-stepper [linear]="isLinear">
<mat-step [stepControl]="firstFormGroup" label="Some Form">
<first-component></first-component>
</mat-step>
<mat-step [stepControl]="secondFormGroup" label="Another Form">
<second-component></second-component>
</mat-step>
<mat-step [stepControl]="thirdFormGroup" label="Review">
<third-component></third-component>
</mat-step>
</mat-horizontal-stepper>
当我尝试此操作时,按下matStepperNext
按钮后会收到以下错误:
TypeError: Cannot read property 'invalid' of undefined
。
答案 0 :(得分:2)
这是最适合我的解决方案。
<mat-horizontal-stepper [linear]="true" #stepper>
<mat-step [stepControl]="selectAdvType">
<ng-template matStepLabel>
<div class="text-center">
<mat-icon>queue_play_next</mat-icon><br /><span>Select Adv Type</span>
</div>
</ng-template>
<app-advertisement-type></app-advertisement-type>
</mat-step>
<mat-step [stepControl]="selectAdvType">
<ng-template matStepLabel>
<div class="text-center">
<mat-icon>directions_car</mat-icon><br /><span>Select Car</span>
</div>
</ng-template>
<app-select-car-adv></app-select-car-adv>
</mat-step>
<mat-step>
<ng-template matStepLabel>
<div class="text-center">
<mat-icon>description</mat-icon><br /><span>Select Features</span>
</div>
</ng-template>
<div>
<button mat-button matStepperPrevious>Back</button>
<button mat-button (click)="stepper.reset()">Reset</button>
</div>
</mat-step>
</mat-horizontal-stepper>
父Ts文件
@Component({
selector: 'app-customer.create.advertisement',
templateUrl: './customer.create.advertisement.component.html',
styleUrls: ['./customer.create.advertisement.component.scss']
})
export class CustomerCreateAdvertisementComponent implements OnInit {
isLinear = false;
selectAdvType: FormGroup;
constructor(private _formBuilder: FormBuilder) { }
ngOnInit() {
this.selectAdvType = this._formBuilder.group({
firstCtrl: ['', Validators.required]
});
}
}
子组件
<form [formGroup]="firstFormGroup">
<ng-template matStepLabel>Fill out your name</ng-template>
<mat-form-field>
<input matInput placeholder="Last name, First name" formControlName="firstCtrl" required>
</mat-form-field>
<div>
<button mat-button matStepperNext>Next</button>
</div>
</form>
@Component({
selector: 'app-advertisement-type',
templateUrl: './advertisement-type.component.html',
styleUrls: ['./advertisement-type.component.scss']
})
export class AdvertisementTypeComponent implements OnInit {
firstFormGroup: FormGroup;
constructor(private _formBuilder: FormBuilder) { }
ngOnInit() {
this.firstFormGroup = this._formBuilder.group({
firstCtrl: ['', Validators.required]
});
}
}
答案 1 :(得分:1)
您可以使用子表单进行解决。 实际上,我几个月前在Angular-UP会议上发表了一篇演讲: https://www.youtube.com/watch?v=sb7tgsNF2Jk
通常的想法是在子组件中创建表单,使用DI注入controlContainer并将本地表单设置为controlContainer表单。
子组件:
@Component({
selector: 'app-company-info',
templateUrl: './company-info.component.html',
styleUrls: ['./company-info.component.scss'],
viewProviders: [{ provide: ControlContainer, useExisting: FormGroupDirective }]
})
export class CompanyInfoComponent implements OnInit {
form: FormGroup;
constructor(
private ctrlContainer: FormGroupDirective,
private fb: FormBuilder) { }
ngOnInit() {
this.form = this.ctrlContainer.form;
this.form.addControl('company',
this.fb.group({
'companyName': this.fb.control(null, [Validators.required]),
'numOfEmployees': this.fb.control(null, [Validators.required])});
}
}
父组件(html):
<mat-horizontal-stepper [linear]="isLinear" #stepper>
<mat-step [stepControl]="companyInfo">
<ng-template matStepLabel>Fill out your name</ng-template>
<form [formGroup]="companyInfo">
<app-company-info></app-company-info>
</form>
<div>
<button mat-button matStepperNext>Next</button>
</div>
</mat-step>
</mat-horizontal-stepper>
父项(ts):
export class WizardComponent implements OnInit {
isLinear = true;
companyInfo: FormGroup;
constructor(private _formBuilder: FormBuilder) {
}
ngOnInit() {
this.companyInfo = this._formBuilder.group({
});
}
}
答案 2 :(得分:1)
改进@ eliran-eliassy答案和@ christian-steinmeyer问题。
Parent.Component.ts
export class ParentComponent implements OnInit {
isLinear = true;
companyInfo: FormGroup;
constructor(private _formBuilder: FormBuilder) {
}
ngOnInit() {
this.companyInfo = this._formBuilder.group({
});
}
}
Parent.Component.html
<mat-horizontal-stepper [linear]="isLinear" #stepper>
<mat-step [stepControl]="companyInfo">
<form [formGroup]="companyInfo">
<ng-template matStepLabel>Fill out your name</ng-template>
<app-company-info></app-company-info>
</form>
<div>
<button mat-button matStepperNext>Next</button>
</div>
</mat-step>
</mat-horizontal-stepper>
Child.Component.ts ->这是子表单
export class ChildComponent implements OnInit {
form: FormGroup;
subForm: FormGroup;
constructor(
private ctrlContainer: FormGroupDirective,
private fb: FormBuilder
) {}
ngOnInit() {
this.subForm = this.fb.group({
companyName: [null, [Validators.required]],
numOfEmployees: [null, [Validators.required]]
});
this.form = this.ctrlContainer.form;
this.form.addControl("company", this.subForm);
}
}
Child.Component.html
<div [formGroup]="subForm">
<mat-form-field appearance="outline">
<input matInput placeholder="Your Company Name" formControlName="companyName">
</mat-form-field>
</div>
在stackblitz上查看此解决方案
答案 3 :(得分:0)
好的,我想我看到了一些问题:
$obj2 = $obj1
需要在 tough-choice.component.ts 文件中定义foodFormGroup 和 pieFormGroup (其中btw在您的示例中拼写错误)代码)
以下是一个示例(来自文档),看看它的外观如何:
<mat-horizontal-stepper [linear]="isLinear">
<mat-step [stepControl]="foodFormGroup">
<food-selection></food-selection>
</mat-step>
<mat-step [stepControl]="pieFormGroup">
<pie-selection></pie-selection>
</mat-step>
</mat-horizontal-stepper>
另外,我在你的例子中没有看到module.ts文件。这就是你想要导入@ angular / material模块的地方,而不是组件文件。
我建议只给一次Angular Material文档。 https://material.angular.io/components/stepper/overview
答案 4 :(得分:0)
在这篇文章中说: angular - Angular Material 2 Stepper Controls
*对不起,如果这不是链接其他帖子的正确方法。