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?
CdkPortalOutlet. Used to add a portal outlet to a template. CdkPortalOutlet is a PortalOutlet . Usage: <!-- Attaches the `userSettingsPortal` from the previous example.
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() .
NgComponentOutletlink Instantiates a Component type and inserts its Host View into the current View. NgComponentOutlet provides a declarative approach for dynamic component creation.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With