我目前对Angular有点新手,我正试图弄清楚如何制作一个简单的步进器(1) - (2) - (3)。
我将html下载为app.component.html
<mat-horizontal-stepper [linear]="true" #stepper>
<mat-step label="Step 1">
step 1
</mat-step>
<mat-step label="Step 2">
step 2
</mat-step>
</mat-horizontal-stepper>
但是,我需要导入什么并使用打字稿来完成这项工作?我有app.component.ts为
import { Component, ViewChild } from '@angular/core';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MatButtonModule } from '@angular/material';
import {FormsModule, ReactiveFormsModule} from '@angular/forms';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
@ViewChild('stepper') stepper;
changeStep(index: number) {
this.stepper.selectedIndex = index;
}
}
答案 0 :(得分:3)
导入角度材质的以下组件并使用与您相同的代码
<强> Module.ts 强>
import { MatStepperModule, MatInputModule, MatButtonModule, MatAutocompleteModule } from '@angular/material';
@NgModule({
imports: [
...
MatStepperModule,
MatInputModule,
MatButtonModule,
MatAutocompleteModule,
],...
<强> HTML 强>
<mat-horizontal-stepper [linear]="isLinear">
<mat-step [stepControl]="firstFormGroup">
<form [formGroup]="firstFormGroup">
<ng-template matStepLabel>Step 1</ng-template>
<button mat-button mat-raised-button color="primary" matStepperNext>Solve</button>
</form>
</mat-step>
<mat-step [stepControl]="secondFormGroup">
<form [formGroup]="secondFormGroup">
<ng-template matStepLabel>Step 2</ng-template>
<mat-form-field>
<input matInput placeholder="Any input" formControlName="secondCtrl" required>
</mat-form-field>
<button mat-button mat-raised-button color="primary" matStepperNext>Next</button>
<button mat-button mat-raised-button color="" matStepperPrevious>Back</button>
</form>
</mat-step>
<mat-step>
<ng-template matStepLabel icon>Done</ng-template>
You are now done.
<button mat-button mat-raised-button color="primary" >Done</button>
</mat-step>
</mat-horizontal-stepper>
<强> Component.ts 强>
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
....
export class ComponentStepper{
isLinear = true;
firstFormGroup: FormGroup;
secondFormGroup: FormGroup;
constructor(private _formBuilder: FormBuilder){}
ngOnInit() {
this.firstFormGroup = this._formBuilder.group({
});
this.secondFormGroup = this._formBuilder.group({
secondCtrl: ['', Validators.required]
});
}
}
即使使用FormGruop验证,这是一个完整的Demo
答案 1 :(得分:0)
随着 Abel Valdez 的回答,不要忘记导入 CdkStepperModule 到 stepper 工作
import { CdkStepperModule } from '@angular/cdk/stepper';
和
imports: [
...
CdkStepperModule,
MatStepperModule,