angular material 步进器在每次点击时动态添加新的步进项
angular material stepper add new step items dynamically on every click
我在我的项目中使用 angular material。我想在每次单击 addItem
按钮时添加额外的步进器项目(mat-step)。
我在这里创建了一个 plunker。 https://stackblitz.com/edit/angular-enmq1z?file=app%2Fstepper-overview-example.ts
谁能帮我解决这个问题?
提前致谢。
我会用FormArray
along with FormGroup
HTML:
<button mat-raised-button (click)="addItem()">
add item
</button>
<form [formGroup]="formGroup">
<mat-horizontal-stepper formArrayName="form">
<mat-step [formGroupName]="i" *ngFor="let customerGroup of formGroup.controls.form.controls; let i = index">
<ng-template matStepLabel>Step {{i + 1}}</ng-template>
<mat-form-field>
<input matInput placeholder="Address" formControlName="cont" required>
</mat-form-field>
<div>
<button mat-button matStepperPrevious>Back</button>
<button mat-button matStepperNext>Next</button>
</div>
</mat-step>
</mat-horizontal-stepper>
</form>
TS代码:
import {Component, OnInit} from '@angular/core';
import {FormBuilder, FormControl,FormGroup, Validators,FormArray} from '@angular/forms';
/**
* @title Stepper overview
*/
@Component({
selector: 'stepper-overview-example',
templateUrl: 'stepper-overview-example.html',
styleUrls: ['stepper-overview-example.css'],
})
export class StepperOverviewExample implements OnInit {
isLinear = false;
formGroup : FormGroup;
form: FormArray;
constructor(private _formBuilder: FormBuilder) {
}
ngOnInit() {
this.formGroup = this._formBuilder.group({
form : this._formBuilder.array([this.init()])
})
this.addItem();
}
init(){
return this._formBuilder.group({
cont :new FormControl('', [Validators.required]),
})
}
addItem(){
this.form = this.formGroup.get('form') as FormArray;
this.form.push(this.init());
}
}
您可以使用 FormGroup 和 FormArray 来存档。
检查下面的例子
我在我的项目中使用 angular material。我想在每次单击 addItem
按钮时添加额外的步进器项目(mat-step)。
我在这里创建了一个 plunker。 https://stackblitz.com/edit/angular-enmq1z?file=app%2Fstepper-overview-example.ts
谁能帮我解决这个问题?
提前致谢。
我会用FormArray
along with FormGroup
HTML:
<button mat-raised-button (click)="addItem()">
add item
</button>
<form [formGroup]="formGroup">
<mat-horizontal-stepper formArrayName="form">
<mat-step [formGroupName]="i" *ngFor="let customerGroup of formGroup.controls.form.controls; let i = index">
<ng-template matStepLabel>Step {{i + 1}}</ng-template>
<mat-form-field>
<input matInput placeholder="Address" formControlName="cont" required>
</mat-form-field>
<div>
<button mat-button matStepperPrevious>Back</button>
<button mat-button matStepperNext>Next</button>
</div>
</mat-step>
</mat-horizontal-stepper>
</form>
TS代码:
import {Component, OnInit} from '@angular/core';
import {FormBuilder, FormControl,FormGroup, Validators,FormArray} from '@angular/forms';
/**
* @title Stepper overview
*/
@Component({
selector: 'stepper-overview-example',
templateUrl: 'stepper-overview-example.html',
styleUrls: ['stepper-overview-example.css'],
})
export class StepperOverviewExample implements OnInit {
isLinear = false;
formGroup : FormGroup;
form: FormArray;
constructor(private _formBuilder: FormBuilder) {
}
ngOnInit() {
this.formGroup = this._formBuilder.group({
form : this._formBuilder.array([this.init()])
})
this.addItem();
}
init(){
return this._formBuilder.group({
cont :new FormControl('', [Validators.required]),
})
}
addItem(){
this.form = this.formGroup.get('form') as FormArray;
this.form.push(this.init());
}
}
您可以使用 FormGroup 和 FormArray 来存档。
检查下面的例子