Warm tip: This article is reproduced from stackoverflow.com, please click
angular angular-template angular-cdk

How is cdkPortal different from ngTemplateOutlet

发布于 2020-03-27 10:31:20

Why should one use cdkPortal over the built-in directives ngTemplateOutlet and ngComponentOutlet in Angular. aren't both of them provide the same functionality? Are there specific features in the CDK Portal that doesn't come with the built-in directives?

Questioner
Murhaf Sousli
Viewed
29
Jota.Toledo 2019-07-04 00:47

If you look at the first example in the cdkPortal docs, you will notice one big advantage/commodity:

cdkPortalOutlet unifies the functionality of ngTemplateOutlet and ngComponentOutlet by allowing devs. to embed both templates and components into the view dynamically.

Furthermore, ComponentPortal allows to dynamically pass Injector instances, which can become handy in cases where you need finer control over the dependency resolution of the to-be-embedded component.