<?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>Angular 2 | Bogdan's blog</title>
	<atom:link href="https://blog.bogdancarpean.com/tag/angular-2/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.bogdancarpean.com</link>
	<description>Code and anything else.</description>
	<lastBuildDate>Sun, 10 Dec 2017 05:48:58 +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>Angular 2 | Bogdan's blog</title>
	<link>https://blog.bogdancarpean.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Fetch data with the new HttpClient on Angular 5</title>
		<link>https://blog.bogdancarpean.com/fetch-data-with-the-new-httpclient-on-angular-5/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=fetch-data-with-the-new-httpclient-on-angular-5</link>
					<comments>https://blog.bogdancarpean.com/fetch-data-with-the-new-httpclient-on-angular-5/#respond</comments>
		
		<dc:creator><![CDATA[Bogdan]]></dc:creator>
		<pubDate>Sun, 10 Dec 2017 05:48:58 +0000</pubDate>
				<category><![CDATA[Angular 2]]></category>
		<category><![CDATA[@angular/common/http]]></category>
		<category><![CDATA[Angular 4]]></category>
		<category><![CDATA[Angular 5]]></category>
		<category><![CDATA[Angular service]]></category>
		<category><![CDATA[fetch data]]></category>
		<category><![CDATA[httpclient]]></category>
		<category><![CDATA[HttpClientModule]]></category>
		<category><![CDATA[injectable]]></category>
		<guid isPermaLink="false">http://blog.bogdancarpean.com/?p=121</guid>

					<description><![CDATA[<p>&#160; 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 The new HttpClient on Angular 5 is here to replace the deprecating @angular/http module. The new HttpClientModule will be imported from @angular/common/http. For the purpose of this article, we are going to use [&#8230;]</p>
The post <a href="https://blog.bogdancarpean.com/fetch-data-with-the-new-httpclient-on-angular-5/">Fetch data with the new HttpClient on Angular 5</a> first appeared on <a href="https://blog.bogdancarpean.com">Bogdan's blog</a>.]]></description>
										<content:encoded><![CDATA[<p>&nbsp;</p>
<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>The new HttpClient on Angular 5 is here to replace the deprecating @angular/http module.</p>
<p>The new HttpClientModule will be imported from @angular/common/http.</p>
<p>For the purpose of this article, we are going to use a service to fetch the data in which we are going to inject the new HttpClient. Our service is going to be injected into the component that is supposed to consume the data.</p>
<p>First, we need to import the HttpClientModule. We need to do this only once at the app.module level.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="js">...
import { HttpClientModule } from '@angular/common/http';

...
@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    ...,
    HttpClientModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }</pre>
<p>After that, we are going to inject the HttpClient into our service and to use it inside a get() function.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="js">import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';

@Injectable()
export class FetchDataService {

  constructor(private _http: HttpClient) { }

  getUsers() {
    return this._http.get('https://yourapi/users');
  }

}</pre>
<p>The getUser() will return an Observable that we are going to treat in our consuming component.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="js">import { Component, OnInit } from '@angular/core';
import { FetchDataService } from '../fetch-data.service';

@Component({
  selector: 'fetch-data',
  templateUrl: './fetch-data.component.html',
  styleUrls: ['./fetch-data.component.scss']
})
export class FetchDataComponent implements OnInit {

  constructor(private _fetchDataService: FetchDataService) { }

  data: any; //any here is just for demo purpose; please change with the type returned by your endpoint

  ngOnInit() {
     this._fetchDataService.getUsers().subscribe(
      data =&gt; { this.data = data},
      err =&gt; console.error(err)
     );
  }

}</pre>
<p>Since getUsers() returns an Observable, we call the subscribe() function to listen and process the returned data.</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/fetch-data-with-the-new-httpclient-on-angular-5/">Fetch data with the new HttpClient on Angular 5</a> first appeared on <a href="https://blog.bogdancarpean.com">Bogdan's blog</a>.]]></content:encoded>
					
					<wfw:commentRss>https://blog.bogdancarpean.com/fetch-data-with-the-new-httpclient-on-angular-5/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<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>How to watch for changes an array/object on Angular 2</title>
		<link>https://blog.bogdancarpean.com/how-to-watch-for-changes-an-arrayobject-on-angular-2/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=how-to-watch-for-changes-an-arrayobject-on-angular-2</link>
					<comments>https://blog.bogdancarpean.com/how-to-watch-for-changes-an-arrayobject-on-angular-2/#comments</comments>
		
		<dc:creator><![CDATA[Bogdan]]></dc:creator>
		<pubDate>Thu, 22 Jun 2017 02:11:21 +0000</pubDate>
				<category><![CDATA[Angular 2]]></category>
		<category><![CDATA[TypeScript]]></category>
		<category><![CDATA[Angular 4]]></category>
		<category><![CDATA[array watch]]></category>
		<category><![CDATA[change detection]]></category>
		<category><![CDATA[object watch]]></category>
		<category><![CDATA[Typescript]]></category>
		<guid isPermaLink="false">http://blog.bogdancarpean.com/?p=79</guid>

					<description><![CDATA[<p>In the good old days of Angular.js we had the $scope.$watch mechanism to follow the changes in our variables. For Angular 2 all this has been changed for performance reasons. So, you will be left wondering how to watch the changes on your array/object, needed on every application. You start googling for a solution. You [&#8230;]</p>
The post <a href="https://blog.bogdancarpean.com/how-to-watch-for-changes-an-arrayobject-on-angular-2/">How to watch for changes an array/object on Angular 2</a> first appeared on <a href="https://blog.bogdancarpean.com">Bogdan's blog</a>.]]></description>
										<content:encoded><![CDATA[<p>In the good old days of Angular.js we had the $scope.$watch mechanism to follow the changes in our variables. For Angular 2 all this has been changed for performance reasons.</p>
<p>So, you will be left wondering how to watch the changes on your array/object, needed on every application. You start googling for a solution. You will find either deprecated info or workarounds like adding an event emitter where your object is updated. No need to mention that this is not a clean solution, neither a reliable one. Let&#8217;s suppose that you have a component that receives a bind data source that you cannot control, what are the options?</p>
<p>So let&#8217;s do this like is supposed to be done, on our component, using the Angular directive dirty check life-cycle hook DoCheck and iterable diffing strategies <span class="typ">IterableDiffers (for arrays) or KeyValueDiffers (for objects), all of them on @Angular/core.</span></p>
<p>Our component will look like the following:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="js">import { Component, Input, IterableDiffers, DoCheck } from '@angular/core';

@Component({
  selector: 'app-array-watch-demo',
  templateUrl: './array-watch-demo.component.html',
  styleUrls: ['./array-watch-demo.component.css']
})
export class ArrayWatchDemoComponent implements DoCheck {

  @Input() datasource: Array&lt;any&gt; = [];

  differ: any;

  constructor(differs: IterableDiffers) {
     this.differ = differs.find([]).create(null);
   }

  ngDoCheck() {
    const change = this.differ.diff(this.datasource);
    console.log(change);
    // here you can do what you want on array change
    // you can check for forEachAddedItem or forEachRemovedItem on change object to see the added/removed items
    // Attention: ngDoCheck() is triggered at each binded variable on componenet; if you have more than one in your component, make sure you filter here the one you want.
  }

}
</pre>
<p>Basically, here we add DoCheck() that is triggered by each bind element change and there we do a diff between the former and actual state on the variable to read the changes.</p>
<p>This solution works on Angular 4.2.3 (June 2017) and is not tested on earlier versions.</p>
<p>I added a demo app on GitHub if you want to see how it works:</p>
<p><a href="https://github.com/bogdancar/angular2-array-object-change-watch-demo">https://github.com/bogdancar/angular2-array-object-change-watch-demo</a></p>The post <a href="https://blog.bogdancarpean.com/how-to-watch-for-changes-an-arrayobject-on-angular-2/">How to watch for changes an array/object on Angular 2</a> first appeared on <a href="https://blog.bogdancarpean.com">Bogdan's blog</a>.]]></content:encoded>
					
					<wfw:commentRss>https://blog.bogdancarpean.com/how-to-watch-for-changes-an-arrayobject-on-angular-2/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
	</channel>
</rss>
