angular - 如何在 mat-stepper 中传递模板

标签 angular angular-material angular-material2

我想这样做:

<ng-template #content>
  <mat-step>step 1</mat-step>
  <mat-step>step 2</mat-step>
</ng-template>

<mat-horizontal-stepper>
  <ng-container *ngTemplateOutlet="content"></ng-container>
</mat-horizontal-stepper>

但是我得到了这个错误

ERROR Error: StaticInjectorError(AppModule)[MatStep -> MatStepper]: StaticInjectorError(Platform: core)[MatStep -> MatStepper]: NullInjectorError: No provider for MatStepper!

我认为这是因为 mat-step 在它的构造函数中注入(inject)了一个 mat-stepper https://github.com/angular/material2/blob/master/src/lib/stepper/stepper.ts#L53

所以我尝试在 ngOutletContext 的上下文中传递 stepper

<mat-horizontal-stepper #ContainerStepper="matHorizontalStepper">
  <ng-container *ngTemplateOutlet="content" ngOutletContext="{$implicit:{stepper:ContainerStepper}}"></ng-container>
</mat-horizontal-stepper>

但这行不通。

有什么想法吗?

最佳答案

你可以试试:

<ng-template #step1Template>
  <form>
    <mat-form-field>
      <input matInput placeholder="Last name, First name" required>
    </mat-form-field>
  </form>
</ng-template>

<ng-template #step2Template>
  <form>
    <mat-form-field>
      <input matInput placeholder="Address" required>
    </mat-form-field>
  </form>
</ng-template>

<mat-horizontal-stepper>
  <mat-step [stepControl]="firstFormGroup">
    <!-- stepper title !-->
    <ng-template matStepLabel>Fill out your name</ng-template>
    <!-- end stepper title !-->

    <!-- stepper content !-->
    <ng-container *ngTemplateOutlet="step1Template"></ng-container>
    <!-- end stepper content !-->

    <!-- stepper footer !-->
    <div>
      <button mat-button matStepperNext>Next</button>
    </div>
    <!-- end stepper footer !-->
  </mat-step>
  <mat-step [stepControl]="secondFormGroup">
    <!-- stepper title !-->
    <ng-template matStepLabel>Fill out your address</ng-template>
    <!-- end stepper title !-->

    <!-- stepper content !-->
    <ng-container *ngTemplateOutlet="step2Template"></ng-container>
    <!-- end stepper content !-->

    <!-- stepper footer !-->
    <div>
      <button mat-button matStepperPrevious>Back</button>
      <button mat-button matStepperNext>Next</button>
    </div>
    <!-- end stepper footer !-->
  </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>

<mat-vertical-stepper>
  <mat-step [stepControl]="firstFormGroup">
    <!-- stepper title !-->
    <ng-template matStepLabel>Fill out your name</ng-template>
    <!-- end stepper title !-->

    <!-- stepper content !-->
    <ng-container *ngTemplateOutlet="step1Template"></ng-container>
    <!-- end stepper content !-->

    <!-- stepper footer !-->
    <div>
      <button mat-button matStepperNext>Next</button>
    </div>
    <!-- end stepper footer !-->
  </mat-step>
  <mat-step [stepControl]="secondFormGroup">
    <!-- stepper title !-->
    <ng-template matStepLabel>Fill out your address</ng-template>
    <!-- end stepper title !-->

    <!-- stepper content !-->
    <ng-container *ngTemplateOutlet="step2Template"></ng-container>
    <!-- end stepper content !-->

    <!-- stepper footer !-->
    <div>
      <button mat-button matStepperPrevious>Back</button>
      <button mat-button matStepperNext>Next</button>
    </div>
    <!-- end stepper footer !-->
  </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-vertical-stepper>

关于angular - 如何在 mat-stepper 中传递模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51305909/

相关文章:

angular - 在服务的构造函数中等待订阅

javascript - 如何使用 Protractor 测试 mdToast 的渲染文本?

css - 设置菜单样式

具有嵌套对象的 Angular Material 2 DataSource 过滤器

html - angular html中输入文本字段的对齐

angular - 单击时取消选中事件的 Angular Material 切换按钮

html - 导航到动态 Angular 2 页面上的指定位置

angular - 如何将 Angular 2 Material 2 中的 md-progress-spinner 颜色绑定(bind)到自定义颜色?

javascript - 带有 Angular Material 的树列表

css - Angular Material mat-menu-item 与 mat-menu 按钮的大小相同