<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Pradip Khadka </title>
    <description>The latest articles on DEV Community by Pradip Khadka  (@coderpradp).</description>
    <link>https://dev.to/coderpradp</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F779909%2F7844228f-ee6e-4736-9ec6-3f43b23ea3db.png</url>
      <title>DEV Community: Pradip Khadka </title>
      <link>https://dev.to/coderpradp</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/coderpradp"/>
    <language>en</language>
    <item>
      <title>Overwrite Portal Outlet</title>
      <dc:creator>Pradip Khadka </dc:creator>
      <pubDate>Fri, 24 Dec 2021 08:24:55 +0000</pubDate>
      <link>https://dev.to/coderpradp/overwrite-portal-outlet-2fm4</link>
      <guid>https://dev.to/coderpradp/overwrite-portal-outlet-2fm4</guid>
      <description>&lt;p&gt;So, I have a custom portal outlet as such:&lt;br&gt;
&lt;code&gt;&amp;lt;div id="title-bar-outlet"&amp;gt;some text content&amp;lt;/div&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And I have a portal component like below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { CdkPortal, DomPortalOutlet } from '@angular/cdk/portal';
import {
  AfterViewInit,
  ApplicationRef,
  Component,
  ComponentFactoryResolver,
  Injector,
  OnDestroy,
  ViewChild
} from '@angular/core';

@Component({
  selector: 'app-title-bar',
  template: `
    &amp;lt;ng-template cdkPortal&amp;gt;
      &amp;lt;ng-content&amp;gt;&amp;lt;/ng-content&amp;gt;
    &amp;lt;/ng-template&amp;gt;
  `
})
export class TitleBarComponent implements AfterViewInit, OnDestroy {
  @ViewChild(CdkPortal)
  private portal: CdkPortal;
  private host: DomPortalOutlet;

  constructor(
    private crf: ComponentFactoryResolver,
    private appRef: ApplicationRef,
    private injector: Injector
  ) {}

  ngAfterViewInit(): void {
    this.host = new DomPortalOutlet(
      document.querySelector('#title-bar-outlet'),
      this.crf,
      this.appRef,
      this.injector
    );
    this.host.attach(this.portal);
  }

  ngOnDestroy(): void {
    this.host.detach();
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now when using the portal like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;app-title-bar&amp;gt;Some title bar content&amp;lt;/app-title-bar&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I want the content in #title-bar-outlet to be replaced by content of app-title-bar component. However, above code just appends component contents to the #title-bar-outlet DOM element&lt;/p&gt;

</description>
      <category>angular</category>
      <category>javascript</category>
      <category>typescript</category>
    </item>
  </channel>
</rss>
