<?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>directive | Bogdan's blog</title>
	<atom:link href="https://blog.bogdancarpean.com/tag/directive/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>directive | 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>
		<item>
		<title>Angular.js share variables between controllers with service</title>
		<link>https://blog.bogdancarpean.com/angular-js-share-variables-between-controllers-with-service/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=angular-js-share-variables-between-controllers-with-service</link>
					<comments>https://blog.bogdancarpean.com/angular-js-share-variables-between-controllers-with-service/#respond</comments>
		
		<dc:creator><![CDATA[Bogdan]]></dc:creator>
		<pubDate>Wed, 10 May 2017 23:15:43 +0000</pubDate>
				<category><![CDATA[Angular]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[angular]]></category>
		<category><![CDATA[controller]]></category>
		<category><![CDATA[directive]]></category>
		<category><![CDATA[scope]]></category>
		<category><![CDATA[share variables]]></category>
		<guid isPermaLink="false">http://blog.bogdancarpean.com/?p=69</guid>

					<description><![CDATA[<p>Note: This article applies to Angular 1 and the example is written in ES5. Let&#8217;s say you have a directive where you have a button and once clicked you want an event to happen in another directive. The problem is that the two directives have different scopes, so passing the change directly is not possible. [&#8230;]</p>
The post <a href="https://blog.bogdancarpean.com/angular-js-share-variables-between-controllers-with-service/">Angular.js share variables between controllers with service</a> first appeared on <a href="https://blog.bogdancarpean.com">Bogdan's blog</a>.]]></description>
										<content:encoded><![CDATA[<p><em>Note: This article applies to Angular 1 and the example is written in ES5.</em></p>
<p>Let&#8217;s say you have a directive where you have a button and once clicked you want an event to happen in another directive. The problem is that the two directives have different scopes, so passing the change directly is not possible. In this approach, we are going to use a service to pass the data between the controllers of the two directives.</p>
<p>For example, we are going to take a directive that has a button that once clicked we display a custom modal. The modal is another directive not related to the one where the click originated.</p>
<p>First, we are going to write the service:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="js" data-enlighter-title="dataService.js">"use strict";

angular.module("someApp").service("dataService", function () {

    var _isModalClicked  = false;

    this.setModalClicked = function(flag){
        _isModalClicked = flag;
    };

    this.getModalClicked = function(){
        return _isModalClicked;
    };
});</pre>
<p>Then, from the controller sending the change (the one attached to button directive), we are going to set the variable on the service (don&#8217;t forget to import the service into the controller):</p>
<pre class="EnlighterJSRAW" data-enlighter-language="js">...
$scope.buttonClick = function(ev){
    dataService.setModalClicked(true);
}
...

</pre>
<p>At this point, we have wired the controller to the service and changed the state of _isModalClicked to true. What is left is to listen to that change inside of the controller consuming the event and to propagate the change, leading to showing the modal. The consuming controller will look like this:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="js">'use strict';

angular.module('someApp')
    .controller('modalCtrl', function ($scope, dataService) {

        $scope.$watch(function() { return dataService.getModalClicked(); }, function(newVal) {
            $scope.showModal = newVal;
        }, true);
    });</pre>
<p>This way, you have a clear separation of concerns when you want to pass a variable change between scopes.</p>The post <a href="https://blog.bogdancarpean.com/angular-js-share-variables-between-controllers-with-service/">Angular.js share variables between controllers with service</a> first appeared on <a href="https://blog.bogdancarpean.com">Bogdan's blog</a>.]]></content:encoded>
					
					<wfw:commentRss>https://blog.bogdancarpean.com/angular-js-share-variables-between-controllers-with-service/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
