angular - 门户主机不会在 Material 2 中渲染模板门户

标签 angular angular-material

演示插件在这里:https://plnkr.co/edit/Ye8MAUmrMEQKnPt93TjT?p=preview

我引用Material 2文档来尝试设置一个在附近主机中渲染的模板门户(实际使用并不是那么直接,我想获取TemplateRef并将其交给另一个组件,但是对于现在我只想让这个简化版本正常工作)。

<ng-template portal #portalEntry>
  <p>hello world</p>
</ng-template>

<ng-template [cdkPortalHost]="portalEntry"></ng-template>

根据文档,这种声明性的、仅标记的实现应该可以工作。但什么也没有呈现。我做错了什么?

最佳答案

cdkPortalHost 应该是 ComponentPortalTemplatePortal 实例:

我怀疑您期望 #portalEntry 将是 TemplatePortalDirective 实例。

@Directive({
  selector: '[cdk-portal], [cdkPortal], [portal]',
  exportAs: 'cdkPortal',
})
export class TemplatePortalDirective extends TemplatePortal {
  constructor(templateRef: TemplateRef<any>, viewContainerRef: ViewContainerRef) {
    super(templateRef, viewContainerRef);
  }
}

但它不会工作,因为在你的情况下 portalEntryTemplateRef

我看到了几个解决这个问题的选项:

1) 使用exportAs

#portalEntry="cdkPortal"

<强> Plunker Example

2) @ViewChild

@ViewChild(TemplatePortalDirective) portalEntry: TemplatePortalDirective;

<强> Plunker Example

关于angular - 门户主机不会在 Material 2 中渲染模板门户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43722955/

相关文章:

html - 如何将不同的颜色应用于 SVG 圆中的 stroke-dashoffset?

Angular2 渲染器 setElementClass isAdd 选项不起作用

javascript - 如何在 Angular 中选择对象列表的不同属性值

javascript - 如何使用基于来自单独组件的 bool 值的 ngif 隐藏/显示组件

Angular Material 2 允许我们制作具有自定义范围的 slider 吗?

angular-material - 使用 CDK 拖放时 Angular Mat-List-Option 布局发生变化

css - 嵌套 fxLayout 容器的好习惯?

angular - Firestore : Passing angularfire2 async data as Ionic 3 navParams not working

Angular 守卫,文档中的陈述不明确

angular - 防止 MatDialog 被拖出窗口