<?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/category/angular-2/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.bogdancarpean.com</link>
	<description>Code and anything else.</description>
	<lastBuildDate>Fri, 24 Jan 2020 19:21:43 +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>New ng lint syntax on Angular 6</title>
		<link>https://blog.bogdancarpean.com/new-ng-lint-syntax-on-angular-6/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=new-ng-lint-syntax-on-angular-6</link>
					<comments>https://blog.bogdancarpean.com/new-ng-lint-syntax-on-angular-6/#respond</comments>
		
		<dc:creator><![CDATA[Bogdan]]></dc:creator>
		<pubDate>Tue, 19 Jun 2018 22:50:36 +0000</pubDate>
				<category><![CDATA[Angular]]></category>
		<category><![CDATA[Angular 2]]></category>
		<category><![CDATA[TypeScript]]></category>
		<category><![CDATA[angular]]></category>
		<category><![CDATA[angular/cli]]></category>
		<category><![CDATA[lint]]></category>
		<guid isPermaLink="false">http://blog.bogdancarpean.com/?p=251</guid>

					<description><![CDATA[<p>Let&#8217;s say you had a syntax like ng lint --format stylish on your package.json before Angular 6. Running this after you update to Angular 6 will result in the following error: Architect command with multiple targets cannot specify overrides. This error is not much of help when you know that before update the linting worked fine. For this [&#8230;]</p>
The post <a href="https://blog.bogdancarpean.com/new-ng-lint-syntax-on-angular-6/">New ng lint syntax on Angular 6</a> first appeared on <a href="https://blog.bogdancarpean.com">Bogdan's blog</a>.]]></description>
										<content:encoded><![CDATA[<div style="text-align: left;">Let&#8217;s say you had a syntax like <code>ng lint --format stylish</code> on your <code>package.json</code> before Angular 6.</div>
<p>Running this after you update to Angular 6 will result in the following error: <code>Architect command with multiple targets cannot specify overrides.</code> This error is not much of help when you know that before update the linting worked fine.</p>
<p>For this to work, we have to add the name of the application after the <code>ng lint</code> command: <code>ng lint your_app_name --format stylish</code></p>The post <a href="https://blog.bogdancarpean.com/new-ng-lint-syntax-on-angular-6/">New ng lint syntax on Angular 6</a> first appeared on <a href="https://blog.bogdancarpean.com">Bogdan's blog</a>.]]></content:encoded>
					
					<wfw:commentRss>https://blog.bogdancarpean.com/new-ng-lint-syntax-on-angular-6/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Angular Service Worker landed on @angular.cli 1.6</title>
		<link>https://blog.bogdancarpean.com/angular-service-worker-landed-on-angular-cli-1-6/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=angular-service-worker-landed-on-angular-cli-1-6</link>
					<comments>https://blog.bogdancarpean.com/angular-service-worker-landed-on-angular-cli-1-6/#respond</comments>
		
		<dc:creator><![CDATA[Bogdan]]></dc:creator>
		<pubDate>Wed, 27 Dec 2017 18:15:08 +0000</pubDate>
				<category><![CDATA[Angular]]></category>
		<category><![CDATA[Angular 2]]></category>
		<category><![CDATA[TypeScript]]></category>
		<category><![CDATA[angular]]></category>
		<category><![CDATA[angular/cli]]></category>
		<category><![CDATA[progressive web apps]]></category>
		<category><![CDATA[service worker]]></category>
		<guid isPermaLink="false">http://blog.bogdancarpean.com/?p=124</guid>

					<description><![CDATA[<p>With Angular 5 version having implemented a new Service Worker customized for Angular apps and @angular/cli 1.6 building it into your Angular app, a further step towards Progressive Web Apps has been made. Why is this important? Progressive Web Apps (or PWA) are seen for many years now as the evolution of apps, bound to [&#8230;]</p>
The post <a href="https://blog.bogdancarpean.com/angular-service-worker-landed-on-angular-cli-1-6/">Angular Service Worker landed on @angular.cli 1.6</a> first appeared on <a href="https://blog.bogdancarpean.com">Bogdan's blog</a>.]]></description>
										<content:encoded><![CDATA[<div style="position: relative;">With Angular 5 version having implemented a new Service Worker customized for Angular apps and @angular/cli 1.6 building it into your Angular app, a further step towards Progressive Web Apps has been made.</div>
<p>Why is this important? Progressive Web Apps (or PWA) are seen for many years now as the evolution of apps, bound to replace the native mobile apps and offer the user a seamless experience (native-like) on mobile platforms. With the announcement of Service Worker API development on Webkit/Safari, the future of across the board support for Service Worker never looked better.</p>
<p><img decoding="async" loading="lazy" class="wp-image-165 size-full aligncenter" src="http://vps277782.vps.ovh.ca/wp-content/uploads/2017/12/CanIUse_ServiceWorker.png" alt="" width="750" height="394" srcset="https://blog.bogdancarpean.com/wp-content/uploads/2017/12/CanIUse_ServiceWorker.png 750w, https://blog.bogdancarpean.com/wp-content/uploads/2017/12/CanIUse_ServiceWorker-300x158.png 300w" sizes="(max-width: 750px) 100vw, 750px" /></p>
<p>What Service Worker is supposed to do? As a building part of a PWA, the Service Worker is a mandatory condition, because is the part of the application that is going to run in the background to handle application requests and caching, permitting the application to function while offline, thus having the native-like behavior.</p>
<p>As of @angular/cli version 1.6, you can create your Angular application containing the Angular Service Worker simply by passing the <code>--service-worker</code> flag when you create the app using the CLI:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="js">ng new yourappname --service-worker</pre>
<p>If you look now into the folder<code>/app</code>, you notice that a new file appeared,<code>ngsw-config</code> which is the configuration file for the added Angular Service Worker. Inside this file, you can decide which assets will be cached and in which manner.</p>
<p>Then, when you build your app, on the folder<code>/dist</code> you will see them <code>ngsw.json</code>and,<code>ngsw-worker.js</code> which is the actual Angular Service Worker.</p>
<p>More info on how to add the Angular Service Worker to your app <a href="https://medium.com/google-developer-experts/a-new-angular-service-worker-creating-automatic-progressive-web-apps-part-2-practice-3221471269a1" target="_blank" rel="noopener noreferrer">here</a>.</p>
<p>If you want to learn more about the Service Worker, <a href="https://developers.google.com/web/fundamentals/primers/service-workers/" target="_blank" rel="noopener noreferrer">here</a> is a good article.</p>The post <a href="https://blog.bogdancarpean.com/angular-service-worker-landed-on-angular-cli-1-6/">Angular Service Worker landed on @angular.cli 1.6</a> first appeared on <a href="https://blog.bogdancarpean.com">Bogdan's blog</a>.]]></content:encoded>
					
					<wfw:commentRss>https://blog.bogdancarpean.com/angular-service-worker-landed-on-angular-cli-1-6/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<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 apply flex on Angular component</title>
		<link>https://blog.bogdancarpean.com/how-to-apply-flex-on-angular-component/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=how-to-apply-flex-on-angular-component</link>
					<comments>https://blog.bogdancarpean.com/how-to-apply-flex-on-angular-component/#respond</comments>
		
		<dc:creator><![CDATA[Bogdan]]></dc:creator>
		<pubDate>Fri, 18 Aug 2017 02:48:41 +0000</pubDate>
				<category><![CDATA[Angular 2]]></category>
		<category><![CDATA[TypeScript]]></category>
		<category><![CDATA[angular]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[flexbox]]></category>
		<category><![CDATA[html]]></category>
		<guid isPermaLink="false">http://blog.bogdancarpean.com/?p=96</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. Supposing you want to use the flexbox CSS property to style your Angular app. So you will have a cascade of flex element that at some [&#8230;]</p>
The post <a href="https://blog.bogdancarpean.com/how-to-apply-flex-on-angular-component/">How to apply flex on Angular component</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>Supposing you want to use the flexbox CSS property to style your Angular app. So you will have a cascade of flex element that at some point will look like this:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="html">&lt;div class="wrapper"&gt; //flex parent
  &lt;angular-component&gt;&lt;/angular-component&gt;
    &lt;div class="children"&gt; //flex children - inside angular component
    //some content
    &lt;/div&gt;
&lt;/div&gt;</pre>
<p>Now you realize that you cannot apply flex property on the child, because the angular-element is cutting the flex tree.</p>
<p>The solution for this problem is to apply flex property on angular-component using `:host` selector on component CSS style:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="css">:host {
    display: flex;
}</pre>
<p>This will apply flex property to the generated DOM node representing the angular-element, thus not breaking the flex tree.</p>The post <a href="https://blog.bogdancarpean.com/how-to-apply-flex-on-angular-component/">How to apply flex on Angular component</a> first appeared on <a href="https://blog.bogdancarpean.com">Bogdan's blog</a>.]]></content:encoded>
					
					<wfw:commentRss>https://blog.bogdancarpean.com/how-to-apply-flex-on-angular-component/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Angular Jasmine Karma test when component has own module</title>
		<link>https://blog.bogdancarpean.com/angular-jasmine-karma-test-when-component-has-own-module/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=angular-jasmine-karma-test-when-component-has-own-module</link>
					<comments>https://blog.bogdancarpean.com/angular-jasmine-karma-test-when-component-has-own-module/#respond</comments>
		
		<dc:creator><![CDATA[Bogdan]]></dc:creator>
		<pubDate>Wed, 09 Aug 2017 16:14:39 +0000</pubDate>
				<category><![CDATA[Angular 2]]></category>
		<category><![CDATA[angular]]></category>
		<category><![CDATA[jasmine]]></category>
		<category><![CDATA[Karma]]></category>
		<category><![CDATA[tdd]]></category>
		<category><![CDATA[tests]]></category>
		<guid isPermaLink="false">http://blog.bogdancarpean.com/?p=91</guid>

					<description><![CDATA[<p>Always remember that your TestBed.configureTestingModule() is there on your test to recreate your module the same way is created on the app. Let&#8217;s assume that you have a structure like following: &#160; - your.component   - your-child1.component   - your.child2.component -your.pipe -your.module In your.module you will import and declare all your three components and the [&#8230;]</p>
The post <a href="https://blog.bogdancarpean.com/angular-jasmine-karma-test-when-component-has-own-module/">Angular Jasmine Karma test when component has own module</a> first appeared on <a href="https://blog.bogdancarpean.com">Bogdan's blog</a>.]]></description>
										<content:encoded><![CDATA[<p>Always remember that your TestBed.configureTestingModule() is there on your test to recreate your module the same way is created on the app.</p>
<p>Let&#8217;s assume that you have a structure like following:</p>
<p>&nbsp;</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">- your.component

  - your-child1.component

  - your.child2.component

-your.pipe

-your.module</pre>
<p>In your.module you will import and declare all your three components and the pipe:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="js">import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { YourComponent }  from './path.to.component';
import { YourChild1Component } from './path.to.component';
import { YourChild2Component } from './path.to.component';
import { YourPipe } from './path.to.component';

@NgModule({
  imports:      [ BrowserModule ],
  declarations: [ YourComponent, YourChild1Component, YourChild2Component, YourPipe ],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }</pre>
<p>&nbsp;</p>
<p>Same way you should do in your test class for your tests to work, you import the components and the pipe and add them to your</p>
<pre class="EnlighterJSRAW" data-enlighter-language="js">TestBed.configureTestModule(declarations: [Your component, YourChild1Component, YourChild2Component, YourPipe])</pre>
<p>&nbsp;</p>The post <a href="https://blog.bogdancarpean.com/angular-jasmine-karma-test-when-component-has-own-module/">Angular Jasmine Karma test when component has own module</a> first appeared on <a href="https://blog.bogdancarpean.com">Bogdan's blog</a>.]]></content:encoded>
					
					<wfw:commentRss>https://blog.bogdancarpean.com/angular-jasmine-karma-test-when-component-has-own-module/feed/</wfw:commentRss>
			<slash:comments>0</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>
