<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>DOM | Bogdan's blog</title>
	<atom:link href="https://blog.bogdancarpean.com/tag/dom/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.bogdancarpean.com</link>
	<description>Code and anything else.</description>
	<lastBuildDate>Thu, 19 Oct 2017 03:42:28 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.1.1</generator>

<image>
	<url>https://blog.bogdancarpean.com/wp-content/uploads/2022/12/icons8-pencil-drawing-96.png</url>
	<title>DOM | Bogdan's blog</title>
	<link>https://blog.bogdancarpean.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Angular how to dynamically inject a component to DOM</title>
		<link>https://blog.bogdancarpean.com/angular-how-to-dynamically-inject-a-component-to-dom/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=angular-how-to-dynamically-inject-a-component-to-dom</link>
					<comments>https://blog.bogdancarpean.com/angular-how-to-dynamically-inject-a-component-to-dom/#comments</comments>
		
		<dc:creator><![CDATA[Bogdan]]></dc:creator>
		<pubDate>Thu, 19 Oct 2017 03:42:28 +0000</pubDate>
				<category><![CDATA[Angular]]></category>
		<category><![CDATA[Angular 2]]></category>
		<category><![CDATA[TypeScript]]></category>
		<category><![CDATA[add component]]></category>
		<category><![CDATA[angular]]></category>
		<category><![CDATA[Angular 4]]></category>
		<category><![CDATA[directive]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[inject component]]></category>
		<category><![CDATA[Typescript]]></category>
		<category><![CDATA[ViewContainerRef]]></category>
		<guid isPermaLink="false">http://blog.bogdancarpean.com/?p=114</guid>

					<description><![CDATA[<p>Note: given the semver adopted from now on by Angular team, I will refer to all versions of Angular2+ as Angular. For the old Angular 1.x, I will use AngularJs In this post, we&#8217;ll take a look on how to dynamically inject a component into the DOM using a directive as a placeholder and ViewContainerRef Angular class. For simplicity, we will declare a new [&#8230;]</p>
The post <a href="https://blog.bogdancarpean.com/angular-how-to-dynamically-inject-a-component-to-dom/">Angular how to dynamically inject a component to DOM</a> first appeared on <a href="https://blog.bogdancarpean.com">Bogdan's blog</a>.]]></description>
										<content:encoded><![CDATA[<p><strong>Note</strong>: <em>given the </em>semver<em> </em>adopted<em> from now on by </em>Angular<em> team, I will refer to all versions of Angular2+ as Angular. For the old Angular </em>1.x,<em> I will use AngularJs</em></p>
<p>In this post, we&#8217;ll take a look on how to dynamically inject a component into the DOM using a directive as a placeholder and ViewContainerRef Angular class.</p>
<p>For simplicity, we will declare a new Angular component that later will be dynamically added/removed to/from the DOM:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="js">import { Component } from '@angular/core';

@Component({
  selector: 'app-injected',
  templateUrl: `&lt;div&gt;Another component was added&lt;/div&gt;`,
  styleUrls: ['./injected.component.css']
})
export class InjectedComponent {

}</pre>
<p>Then we declare the directive used as a placeholder on the DOM where we want to inject the new component:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="js">import { Directive, ViewContainerRef } from '@angular/core';

@Directive({
  selector: '[appInject]'
})
export class InjectDirective {

  constructor(public viewContainerRef: ViewContainerRef) { }

}</pre>
<p>We add the directive on DOM in the place where the new component will be injected:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="html">&lt;div class="where_you_want_to_inject"&gt;    
  &lt;ng-template appInject&gt;&lt;/ng-template&gt;
&lt;/div&gt;</pre>
<p>Finally, into the parent component, we add two functions, one who add the component, other who remove it:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="js">import { Component, ViewChild, ComponentFactoryResolver } from '@angular/core';
import { InjectDirective } from '../inject.directive';
import { InjectedComponent } from '../injected/injected.component';

@Component({
  selector: 'app-parent',
  templateUrl: './parent.component.html',
  styleUrls: ['./parent.component.css']
})
export class ParentComponent {

  @ViewChild(InjectDirective) injectComp: InjectDirective;

  constructor(private _componentFactoryResolver: ComponentFactoryResolver) {
  }

  public addComp() {
    const componentFactory = this._componentFactoryResolver.resolveComponentFactory(InjectedComponent);
    const viewContainerRef = this.injectComp.viewContainerRef;
    const componentRef = viewContainerRef.createComponent(componentFactory);
  }

  public removeComp() {
    const componentFactory = this._componentFactoryResolver.resolveComponentFactory(InjectedComponent);
    const viewContainerRef = this.injectComp.viewContainerRef;
    const componentRef = viewContainerRef.remove();
  }

}</pre>
<p>This way we can dynamically add/remove components.</p>
<p>If you need an example, I added a working demo app on Github <a href="https://github.com/bogdancar/angular2-dynamically-add-component-to-dom" target="_blank" rel="noopener noreferrer">here</a>.</p>
<p>&nbsp;</p>
<p><em>If your company needs consulting on migration from Angular 1 to Angular 2/4/5 or training/update Angular knowledge, write me to <a href="mailto:contact@bogdancarpean.com">contact@bogdancarpean.com</a></em></p>The post <a href="https://blog.bogdancarpean.com/angular-how-to-dynamically-inject-a-component-to-dom/">Angular how to dynamically inject a component to DOM</a> first appeared on <a href="https://blog.bogdancarpean.com">Bogdan's blog</a>.]]></content:encoded>
					
					<wfw:commentRss>https://blog.bogdancarpean.com/angular-how-to-dynamically-inject-a-component-to-dom/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
