<?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>array watch | Bogdan's blog</title>
	<atom:link href="https://blog.bogdancarpean.com/tag/array-watch/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.bogdancarpean.com</link>
	<description>Code and anything else.</description>
	<lastBuildDate>Thu, 22 Jun 2017 02:11:21 +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>array watch | Bogdan's blog</title>
	<link>https://blog.bogdancarpean.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<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>
