How to set context on an Angular [cdkPortalOutlet]

好久不见. 提交于 2019-12-06 11:04:16

I think you can assign the context directly to the portal but I haven't tested this. It has to be assigned before the outlet uses the portal and that might be tricky.

@ViewChild(CdkPortal) myPortalRef: CdkPortal;  

public ngAfterViewInit(): void {
    this.myPortalRef.context = {...}; // your context data
}

I think the issue here is that the context is attached to the portal instead of being independant. So you can't use the same portal to render in two different outlets with two different contexts.

Also, have you tried using cdkPortalOutlet with the micro-syntax parser in Angular to see if it works?

<ng-template *cdkPortalOutlet="myPortalRef; context: contextExp"></ng-template>

From what I can tell the ng-template methods for the portals isn't used in the library. Everything with portals is done as services, and the template variant appears to have been an add on feature for the community but it wasn't fully vetted.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!