Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is cdkPortal different from ngTemplateOutlet

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?

like image 738
Murhaf Sousli Avatar asked Jul 03 '19 15:07

Murhaf Sousli


People also ask

What is CdkPortalOutlet?

CdkPortalOutlet. Used to add a portal outlet to a template. CdkPortalOutlet is a PortalOutlet . Usage: <!-- Attaches the `userSettingsPortal` from the previous example.

What is Templateref?

TemplateReflink Represents an embedded template that can be used to instantiate embedded views. To instantiate embedded views based on a template, use the ViewContainerRef method createEmbeddedView() .

What is NgComponentOutlet?

NgComponentOutletlink Instantiates a Component type and inserts its Host View into the current View. NgComponentOutlet provides a declarative approach for dynamic component creation.

Can we use ng template inside ng template?

ng-template is an Angular element that is used for rendering HTML in a template. However, it is not rendered directly on DOM. If you include an ng-template tag to a template, the tag and the content inside it will be replaced by comment upon render.


1 Answers

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.

like image 108
Jota.Toledo Avatar answered Oct 01 '22 03:10

Jota.Toledo