如何在 Angular 5 中使用材料步进器

use*_*109 1 javascript frontend material-design angular-material2 angular

我目前对 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>
Run Code Online (Sandbox Code Playgroud)

但是,我需要在打字稿中导入和执行哪些操作才能使其工作?我有 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;
  }
}
Run Code Online (Sandbox Code Playgroud)

Abe*_*dez 5

导入以下角度材料组件并使用您拥有的相同代码

模块.ts

import { MatStepperModule, MatInputModule, MatButtonModule, MatAutocompleteModule } from '@angular/material';


@NgModule({
    imports: [
        ...    
        MatStepperModule,
        MatInputModule,
        MatButtonModule,
        MatAutocompleteModule,                        
    ],...
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

组件.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]
    });
  }
}
Run Code Online (Sandbox Code Playgroud)

即使使用 FormGruop 验证,这也是一个完整的演示