要创建一个Angular 7的多步表单,可以按照以下步骤进行操作:
ng new multi-step-form
cd multi-step-form
npm install --save @angular/material @angular/cdk @angular/animations
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { MatStepperModule } from '@angular/material/stepper';
import { MatButtonModule } from '@angular/material/button';
import { MatInputModule } from '@angular/material/input';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule,
BrowserAnimationsModule,
FormsModule,
ReactiveFormsModule,
MatStepperModule,
MatButtonModule,
MatInputModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
ng generate component multi-step-form
Step 3
Thank you for submitting the form.
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
firstFormGroup: FormGroup;
secondFormGroup: FormGroup;
constructor(private formBuilder: FormBuilder) {}
ngOnInit() {
this.firstFormGroup = this.formBuilder.group({
firstName: ['', Validators.required],
lastName: ['', Validators.required]
});
this.secondFormGroup = this.formBuilder.group({
email: ['', Validators.required],
phone: ['', Validators.required]
});
}
submitForm() {
// Submit the form logic here
}
}
现在,你已经成功创建了一个Angular 7的多步表单。可以使用"ng serve"命令在浏览器中运行应用程序,并查看多步表单的效果。