gpt4 book ai didi

javascript - 如何在 Angular 4 Material 的 Stepper 中提交表单

转载 作者:行者123 更新时间:2023-11-30 14:37:57 25 4
gpt4 key购买 nike

如何在 Angular 材的步进器中提交表单数据。我正在关注 Angular Material 的示例 https://material.angular.io/components/stepper/examples .在问这个问题之前我做了很多谷歌搜索,但没有找到任何答案。

<mat-horizontal-stepper [linear]="isLinear" #stepper="matHorizontalStepper">
<mat-step [stepControl]="firstFormGroup">
<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>
</mat-step>
<mat-step [stepControl]="secondFormGroup">
<form [formGroup]="secondFormGroup">
<ng-template matStepLabel>Fill out your address</ng-template>
<mat-form-field>
<input matInput placeholder="Address" formControlName="secondCtrl" required>
</mat-form-field>
<div>
<button mat-button matStepperPrevious>Back</button>
<button mat-button matStepperNext>Next</button>
</div>
</form>
</mat-step>
<mat-step>
<ng-template matStepLabel>Done</ng-template>
You are now done.
<div>
<button mat-button matStepperPrevious>Back</button>
<button mat-button (click)="stepper.reset()">Reset</button>
</div>
</mat-step>
</mat-horizontal-stepper>

我完成了两份表格的填写。但在那之后我不知道如何获取/提交表单数据。

谢谢你的帮助...:-)

最佳答案

在 Stepper 中有表单的地方提供提交按钮和 ngSubmit

      <button mat-raised-button (click)="isLinear = true" id="toggle-linear">Enable linear mode</button>

<mat-horizontal-stepper [linear]="isLinear" #stepper="matHorizontalStepper">
<mat-step [stepControl]="firstFormGroup">
<form [formGroup]="firstFormGroup" (ngSubmit)="form1()" #formone="ngForm">
<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 type="button" mat-button matStepperNext>Next</button>
<button type="submit" mat-button>submit</button>
</div>
</form>
</mat-step>
<mat-step [stepControl]="secondFormGroup">
<form [formGroup]="secondFormGroup" (ngSubmit)="form2()" #formtwo="ngForm">
<ng-template matStepLabel>Fill out your address</ng-template>
<mat-form-field>
<input matInput placeholder="Address" formControlName="secondCtrl" required>
</mat-form-field>
<div>
<button type="button" mat-button matStepperPrevious>Back</button>
<button type="button" mat-button matStepperNext>Next</button>
<button type="submit" mat-button>submit</button>
</div>
</form>
</mat-step>
<mat-step>
<ng-template matStepLabel>Done</ng-template>
You are now done.
<div>
<button mat-button matStepperPrevious>Back</button>
<button mat-button type="button" (click)="stepper.reset()">Reset</button>
<button mat-button type="button" (click)="formone.ngSubmit.emit();formtwo.ngSubmit.emit()">
submit
</button>
</div>
</mat-step>
</mat-horizontal-stepper>

组件

form1(){
console.log(this.firstFormGroup.value);
}

form2(){
console.log(this.secondFormGroup.value);
}

Working Demo

关于javascript - 如何在 Angular 4 Material 的 Stepper 中提交表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50133336/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com