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

时间:2018-05-14 20:37:27

标签: javascript angular material-design frontend angular-material2

我目前对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;
  }
}

2 个答案:

答案 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,