【发布时间】:2018-12-20 16:49:04
【问题描述】:
我想这样做:
<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>
但我得到了这个错误
错误错误:StaticInjectorError(AppModule)[MatStep -> MatStepper]: StaticInjectorError(平台:核心)[MatStep -> MatStepper]: NullInjectorError: MatStepper 没有提供者!
我认为这是因为 mat-step 在它的构造函数中注入了一个 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>
但这不起作用。
有什么想法吗?
【问题讨论】:
-
解释你为什么需要这样做以及你想要达到的目标。您可以将整个 mat-horizontal-stepper 放在模板中,并获得与您的代码相同的结果。
-
我需要步进器为垫水平步进器或垫垂直步进器,具体取决于设备布局。步进器中的所有逻辑都保持不变,我不想编写两次代码(约 150 行)。通常,我会有 2 个步进器(水平和垂直),并带有一个 *ngIf,对应于布局。在那些步进器中,我不想复制所有代码
-
这是一个很好的理由!但目前还不能做你想做的事:github.com/angular/material2/issues/8014。
-
尝试做完全相同的事情。您是否找到了不涉及复制步进器的解决方案?
-
不,不幸的是代码重复是不可避免的
标签: angular angular-material angular-material2