<?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>Forem: Khiko</title>
    <description>The latest articles on Forem by Khiko (@hikobrat).</description>
    <link>https://forem.com/hikobrat</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%2F1262794%2Fe4754e9d-4ea8-418b-8289-433133519f31.png</url>
      <title>Forem: Khiko</title>
      <link>https://forem.com/hikobrat</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/hikobrat"/>
    <language>en</language>
    <item>
      <title>Issue with Accessing TemplateRef in Angular Component.</title>
      <dc:creator>Khiko</dc:creator>
      <pubDate>Mon, 22 Jan 2024 09:34:41 +0000</pubDate>
      <link>https://forem.com/hikobrat/issue-with-accessing-templateref-in-angular-component-36h</link>
      <guid>https://forem.com/hikobrat/issue-with-accessing-templateref-in-angular-component-36h</guid>
      <description>&lt;p&gt;I'm facing difficulties in accessing TemplateRef in an Angular component.  The problem is occuring when I'm using two directives in one ng-container. &lt;br&gt;
Briefly about the work of my library the developer who uses the lib should add his components as TemplateRef(using directive) and i'm saving all templates in my service state. After when lib is starting work I'm adding that templateкуа to lib-component using ViewContainerRef (viewContainer.createEmbeddedView(templateRef)) (until this period everything is OK).&lt;br&gt;
For sometimes I need to periodically update templateRef (there is i'm having trouble). What I'm trying to do i will demonstrate below.&lt;/p&gt;

&lt;p&gt;There is how developer adds his templateRef&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;div *libTemplate="let pane; named: 'cicdTemplate'"&amp;gt;
  &amp;lt;div&amp;gt;
    &amp;lt;app-new-win-settings&amp;gt;&amp;lt;/app-new-win-settings&amp;gt;
    &amp;lt;button (click)="setDefaultToolbar()"&amp;gt;Set Default Toolbar&amp;lt;/button&amp;gt;
    CI / CD template
    {{ draggingData | json }}
  &amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;

&amp;lt;div *libTemplate="let pane; named: 'projectsTemplate'"&amp;gt;
  &amp;lt;div&amp;gt;
    Projects template
    &amp;lt;button (click)="handleAddToolWindowClick()"&amp;gt;Create new Tool Window&amp;lt;/button&amp;gt;
  &amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The libDirective&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Directive({
  selector: '[libTemplate]',
})
export class LibTemplateDirective implements AfterContentInit, OnDestroy {
  private name: string | undefined;

  /** Stores the name to register this template under */
  @Input()
  public set libTemplateNamed(name: string) {
    this.name = name;
  }

  /**
   * Construct a new pane template directive.
   * @param templateRef injected to be registered with the pane template service
   * @param _viewContainer injected (unused)
   */
  public constructor(
    private readonly templateRef: TemplateRef&amp;lt;any&amp;gt;,
    _viewContainer: ViewContainerRef,
    private templateService: TemplateService,
  ) {}

  /** Register the pane template */
  public ngAfterContentInit(): void {
    if (this.name === undefined) {
      throw new Error(`screen template missing 'named' keyword`);
    }
    this.templateService.add(this.name, { template: this.templateRef });
  }

  /** Attempt to unregister the template */
  public ngOnDestroy(): void {
    if (this.name === undefined) {
      return;
    }
    this.templateService.remove(this.name, { template: this.templateRef });
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The libComponent&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//html

&amp;lt;ng-container omScreenRenderer&amp;gt;&amp;lt;/ng-container&amp;gt;

//ts

export class LibComponent implements AfterViewInit {
  private _template: any | undefined;

constructor(private templateService: TemplateService) {
templateService.get(name).subscribe((templateRef) =&amp;gt;{
this._template = templateRef
})
}

  ngAfterViewInit(): void {
    this.container.viewContainer.clear();
    if (this._template !== undefined) {
     const viewRef =this.container.viewContainer.createEmbeddedView(this._template);
    }

// there i need periodiaclly update the templateRef
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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