<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Unni&#039;s space</title>
	<atom:link href="http://csharprambling.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://csharprambling.wordpress.com</link>
	<description>FeF &#38; C# Rambling</description>
	<lastBuildDate>Tue, 16 Apr 2013 01:47:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='csharprambling.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://1.gravatar.com/blavatar/16149c3eb0eb7ed31f583cec44ba0721?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>Unni&#039;s space</title>
		<link>http://csharprambling.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://csharprambling.wordpress.com/osd.xml" title="Unni&#039;s space" />
	<atom:link rel='hub' href='http://csharprambling.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Silverlight to Angular &#8211; 7 (Directives &#8211; I)</title>
		<link>http://csharprambling.wordpress.com/2013/04/15/silverlight-to-angular-7-directives-i/</link>
		<comments>http://csharprambling.wordpress.com/2013/04/15/silverlight-to-angular-7-directives-i/#comments</comments>
		<pubDate>Mon, 15 Apr 2013 16:22:00 +0000</pubDate>
		<dc:creator>Unni</dc:creator>
				<category><![CDATA[AngularJS]]></category>
		<category><![CDATA[Directive]]></category>

		<guid isPermaLink="false">https://csharprambling.wordpress.com/?p=500</guid>
		<description><![CDATA[Even though I have Silverlight in the blog heading, we part ways with Silverlight in the last blog. Now, we are in Angular world. Ask any of the Angular user, which is their favorite feature in Angular? 99% of them &#8230; <a href="http://csharprambling.wordpress.com/2013/04/15/silverlight-to-angular-7-directives-i/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=csharprambling.wordpress.com&#038;blog=18899480&#038;post=500&#038;subd=csharprambling&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Even though I have Silverlight in the blog heading, we part ways with Silverlight in the last <a href="http://csharprambling.wordpress.com/2013/04/12/silverlight-to-angular-6-unit-testing/">blog</a>. Now, we are in Angular world. Ask any of the Angular user, which is their favorite feature in Angular? 99% of them will say, ‘Directives’. Without doubt, it is one of my favorite feature as well. In the beginning, when we started out with Angular, I mentioned that Angular can teach static HTML some new tricks like new elements, attributes etc., and it is done with Directives. It is without doubt that sets Angular apart from all the frameworks or toolsets. </p>
<p>I was going to create a step by step instruction for Hello Word directive but <a href="http://www.youtube.com/watch?v=Yg-R1gchccg">John Lindquist</a> beat me to it. So instead of me doing ‘Hello World’, we will look at creating a directive for addition example we have been doing in couple of our examples. I am planning on doing it in three parts.</p>
<ol>
<li>Create a simple directive which shows static content.</li>
<li>Create a simple directive which accepts input from HTML.</li>
<li>Create a directive with binding and control hook up.</li>
</ol>
<p>Lets start out by looking at our existing example;</p>
<p><strong>View:</strong></p>
<div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:9b5440ff-ab02-4f8b-9923-409b0f55ed5f" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<pre style="width:859px;height:294px;background-color:White;overflow:auto;"><div><span style="color:#008080;"> 1</span> <span style="color:#0000FF;">&lt;!</span><span style="color:#FF00FF;">DOCTYPE html</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 2</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">html </span><span style="color:#FF0000;">ng-app</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 3</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">head</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 4</span> <span style="color:#000000;">    </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">title</span><span style="color:#0000FF;">&gt;&lt;/</span><span style="color:#800000;">title</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 5</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">head</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 6</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">body</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 7</span> <span style="color:#000000;">    </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">div </span><span style="color:#FF0000;">ng-controller</span><span style="color:#0000FF;">=&quot;additionCtrl&quot;</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 8</span> <span style="color:#000000;">        First Number: </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">input </span><span style="color:#FF0000;">ng-model</span><span style="color:#0000FF;">=&quot;NumberOne&quot;</span><span style="color:#0000FF;">/&gt;&lt;</span><span style="color:#800000;">br</span><span style="color:#0000FF;">/&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 9</span> <span style="color:#000000;">        Second Number: </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">input </span><span style="color:#FF0000;">ng-model</span><span style="color:#0000FF;">=&quot;NumberTwo&quot;</span><span style="color:#0000FF;">/&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">10</span> <span style="color:#000000;">        </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">button </span><span style="color:#FF0000;">ng-click</span><span style="color:#0000FF;">=&quot;add(NumberOne, NumberTwo)&quot;</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">Add</span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">button</span><span style="color:#0000FF;">&gt;&lt;</span><span style="color:#800000;">br</span><span style="color:#0000FF;">/&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">11</span> <span style="color:#000000;">        Result: {{Result}}
</span><span style="color:#008080;">12</span> <span style="color:#000000;">    </span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">div</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">13</span> <span style="color:#000000;">    </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">script </span><span style="color:#FF0000;">src</span><span style="color:#0000FF;">=&quot;https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js&quot;</span><span style="color:#0000FF;">&gt;&lt;/</span><span style="color:#800000;">script</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">14</span> <span style="color:#000000;">    </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">script </span><span style="color:#FF0000;">src</span><span style="color:#0000FF;">=&quot;Scripts/additionCtrl.js&quot;</span><span style="color:#0000FF;">&gt;&lt;/</span><span style="color:#800000;">script</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">15</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">body</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">16</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">html</span><span style="color:#0000FF;">&gt;</span></div></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p><strong>Controller:</strong></p>
<div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:2888142b-dfb1-4f08-8453-e988363dca67" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<pre style="width:859px;height:148px;background-color:White;overflow:auto;"><div><span style="color:#008080;">1</span> <span style="color:#0000FF;">function</span><span style="color:#000000;"> additionCtrl($scope) {
</span><span style="color:#008080;">2</span> <span style="color:#000000;">    $scope.NumberOne </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">0</span><span style="color:#000000;">;
</span><span style="color:#008080;">3</span> <span style="color:#000000;">    $scope.NumberTwo </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">0</span><span style="color:#000000;">;
</span><span style="color:#008080;">4</span> <span style="color:#000000;">    $scope.Result </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">0</span><span style="color:#000000;">;
</span><span style="color:#008080;">5</span> <span style="color:#000000;">    $scope.add </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#0000FF;">function</span><span style="color:#000000;"> (a, b) {
</span><span style="color:#008080;">6</span> <span style="color:#000000;">        $scope.Result </span><span style="color:#000000;">=</span><span style="color:#000000;"> Number(a) </span><span style="color:#000000;">+</span><span style="color:#000000;"> Number(b);
</span><span style="color:#008080;">7</span> <span style="color:#000000;">    };
</span><span style="color:#008080;">8</span> <span style="color:#000000;">}</span></div></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>In the current code, there are no directives, it nothing but our simple addition program. We are going to first modify our code to create a directive, similar to ‘Hello Word’ . This will be new directive which will create a new HTML element, which will display ‘First Number’. Similar to <a href="http://csharprambling.wordpress.com/2013/04/09/silverlight-to-angular-5-ivalueconverter-filter/">Filters</a>, <em>we will create directive at module level</em>. Lets start by creating a simple directive;</p>
<p><strong>Problem: Create a new HTML element, which will display ‘First Number’. Call the new element as ‘display-number’.</strong></p>
<p>Lets start with the test so we know what are we trying to do and also learn the TDD along the way. I am still not very good at Javascript so I might be not be following some standard pattern, which I hope someone will help me correct.</p>
<p><strong>Test:</strong></p>
<p>Following our previous <a href="http://csharprambling.wordpress.com/2013/04/12/silverlight-to-angular-6-unit-testing/">unit test model</a>, we will create two folders, one for<strong> App</strong> and another one for <strong>Test</strong> so that we can separate the code that goes into production. We will copy the SpecRunner.html from our previous test project and which is similar to the following</p>
<div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:598036ba-b5ad-470c-ada5-748b01b5c57d" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<pre style="width:859px;height:428px;background-color:White;overflow:auto;"><div><span style="color:#008080;"> 1</span> <span style="color:#0000FF;">&lt;!</span><span style="color:#FF00FF;">DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;
</span><span style="color:#008080;"> 2</span> <span style="color:#FF00FF;">  &quot;http://www.w3.org/TR/html4/loose.dtd&quot;</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 3</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">html</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 4</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">head</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 5</span> <span style="color:#000000;">  </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">title</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">Jasmine Spec Runner</span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">title</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 6</span> <span style="color:#000000;">    
</span><span style="color:#008080;"> 7</span> <span style="color:#000000;">  </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">link </span><span style="color:#FF0000;">rel</span><span style="color:#0000FF;">=&quot;shortcut icon&quot;</span><span style="color:#FF0000;"> type</span><span style="color:#0000FF;">=&quot;image/png&quot;</span><span style="color:#FF0000;"> href</span><span style="color:#0000FF;">=&quot;http://searls.github.com/jasmine-all/jasmine_favicon.png&quot;</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 8</span> <span style="color:#000000;">  </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">link </span><span style="color:#FF0000;">rel</span><span style="color:#0000FF;">=&quot;stylesheet&quot;</span><span style="color:#FF0000;"> type</span><span style="color:#0000FF;">=&quot;text/css&quot;</span><span style="color:#FF0000;"> href</span><span style="color:#0000FF;">=&quot;C:\temp\jasmine\lib\jasmine-1.3.1\jasmine.css&quot;</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 9</span> <span style="color:#000000;">  </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">script </span><span style="color:#FF0000;">type</span><span style="color:#0000FF;">=&quot;text/javascript&quot;</span><span style="color:#FF0000;"> src</span><span style="color:#0000FF;">=&quot;http://searls.github.com/jasmine-all/jasmine-all-min.js&quot;</span><span style="color:#0000FF;">&gt;&lt;/</span><span style="color:#800000;">script</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">10</span> <span style="color:#000000;">  </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">script </span><span style="color:#FF0000;">type</span><span style="color:#0000FF;">=&quot;text/javascript&quot;</span><span style="color:#FF0000;"> src</span><span style="color:#0000FF;">=&quot;https://github.com/akiellor/rhino-hax/blob/master/modules/jasmine-console-runner.coffee&quot;</span><span style="color:#0000FF;">&gt;&lt;/</span><span style="color:#800000;">script</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">     
</span><span style="color:#008080;">11</span> <span style="color:#000000;">
</span><span style="color:#008080;">12</span> <span style="color:#000000;">    </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">script </span><span style="color:#FF0000;">type</span><span style="color:#0000FF;">=&quot;text/javascript&quot;</span><span style="color:#FF0000;"> src</span><span style="color:#0000FF;">=&quot;http://code.angularjs.org/1.0.6/angular.js&quot;</span><span style="color:#0000FF;">&gt;&lt;/</span><span style="color:#800000;">script</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">13</span> <span style="color:#000000;">    </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">script </span><span style="color:#FF0000;">type</span><span style="color:#0000FF;">=&quot;text/javascript&quot;</span><span style="color:#FF0000;"> src</span><span style="color:#0000FF;">=&quot;http://code.angularjs.org/1.0.6/angular-mocks.js&quot;</span><span style="color:#0000FF;">&gt;&lt;/</span><span style="color:#800000;">script</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">14</span> <span style="color:#000000;">
</span><span style="color:#008080;">15</span> <span style="color:#000000;">  </span><span style="color:#008000;">&lt;!--</span><span style="color:#008000;"> include source files here... </span><span style="color:#008000;">--&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">16</span> <span style="color:#000000;">  </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">script </span><span style="color:#FF0000;">type</span><span style="color:#0000FF;">=&quot;text/javascript&quot;</span><span style="color:#FF0000;"> src</span><span style="color:#0000FF;">=&quot;../App/scripts/additionCtrl.js&quot;</span><span style="color:#0000FF;">&gt;&lt;/</span><span style="color:#800000;">script</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">17</span> <span style="color:#000000;">
</span><span style="color:#008080;">18</span> <span style="color:#000000;">  </span><span style="color:#008000;">&lt;!--</span><span style="color:#008000;"> include spec files here... </span><span style="color:#008000;">--&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">19</span> <span style="color:#000000;">  </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">script </span><span style="color:#FF0000;">type</span><span style="color:#0000FF;">=&quot;text/javascript&quot;</span><span style="color:#FF0000;"> src</span><span style="color:#0000FF;">=&quot;spec/displayNumber.spec.js&quot;</span><span style="color:#0000FF;">&gt;&lt;/</span><span style="color:#800000;">script</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">    
</span><span style="color:#008080;">20</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">head</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">21</span> <span style="color:#000000;">
</span><span style="color:#008080;">22</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">body</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">23</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">body</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">24</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">html</span><span style="color:#0000FF;">&gt;</span></div></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>Line (8) – Jasmine downloaded location will have the css file.</p>
<p>Line (16) – points to our controller where we will do addition of&#160; two numbers.</p>
<p>Line (19) – This is where we are going to put our test spec.</p>
<p>Following code is our displayNumber.spec.js</p>
<div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:eab9cb93-12ae-456f-a30b-60788a95c98c" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<pre style="width:859px;height:428px;background-color:White;overflow:auto;"><div><span style="color:#008080;"> 1</span> <span style="color:#000000;">describe(</span><span style="color:#000000;">'</span><span style="color:#000000;">displayNumber</span><span style="color:#000000;">'</span><span style="color:#000000;">, </span><span style="color:#0000FF;">function</span><span style="color:#000000;"> () {
</span><span style="color:#008080;"> 2</span> <span style="color:#000000;">    </span><span style="color:#0000FF;">var</span><span style="color:#000000;"> element, scope;
</span><span style="color:#008080;"> 3</span> <span style="color:#000000;">    
</span><span style="color:#008080;"> 4</span> <span style="color:#000000;">    beforeEach(module(</span><span style="color:#000000;">&quot;</span><span style="color:#000000;">additionApp</span><span style="color:#000000;">&quot;</span><span style="color:#000000;">));
</span><span style="color:#008080;"> 5</span> <span style="color:#000000;">
</span><span style="color:#008080;"> 6</span> <span style="color:#000000;">    beforeEach(inject(</span><span style="color:#0000FF;">function</span><span style="color:#000000;">($rootScope, $compile) {
</span><span style="color:#008080;"> 7</span> <span style="color:#000000;">        element </span><span style="color:#000000;">=</span><span style="color:#000000;"> angular.element(</span><span style="color:#000000;">'</span><span style="color:#000000;">&lt;display-number&gt;&lt;/display-number&gt;</span><span style="color:#000000;">'</span><span style="color:#000000;">);
</span><span style="color:#008080;"> 8</span> <span style="color:#000000;">        scope </span><span style="color:#000000;">=</span><span style="color:#000000;"> $rootScope;
</span><span style="color:#008080;"> 9</span> <span style="color:#000000;">        $compile(element)(scope);
</span><span style="color:#008080;">10</span> <span style="color:#000000;">        scope.$digest();
</span><span style="color:#008080;">11</span> <span style="color:#000000;">    }));
</span><span style="color:#008080;">12</span> <span style="color:#000000;">
</span><span style="color:#008080;">13</span> <span style="color:#000000;">    it(</span><span style="color:#000000;">'</span><span style="color:#000000;">should create displayNumber span element</span><span style="color:#000000;">'</span><span style="color:#000000;">, </span><span style="color:#0000FF;">function</span><span style="color:#000000;"> () {
</span><span style="color:#008080;">14</span> <span style="color:#000000;">        </span><span style="color:#0000FF;">var</span><span style="color:#000000;"> elem </span><span style="color:#000000;">=</span><span style="color:#000000;"> element.find(</span><span style="color:#000000;">&quot;</span><span style="color:#000000;">span</span><span style="color:#000000;">&quot;</span><span style="color:#000000;">);
</span><span style="color:#008080;">15</span> <span style="color:#000000;">        expect(elem.length).toEqual(</span><span style="color:#000000;">1</span><span style="color:#000000;">);
</span><span style="color:#008080;">16</span> <span style="color:#000000;">    });
</span><span style="color:#008080;">17</span> <span style="color:#000000;">
</span><span style="color:#008080;">18</span> <span style="color:#000000;">    it(</span><span style="color:#000000;">'</span><span style="color:#000000;">should create displayNumber span element with &quot;First Number&quot; as text</span><span style="color:#000000;">'</span><span style="color:#000000;">, </span><span style="color:#0000FF;">function</span><span style="color:#000000;"> () {
</span><span style="color:#008080;">19</span> <span style="color:#000000;">        </span><span style="color:#0000FF;">debugger</span><span style="color:#000000;">;
</span><span style="color:#008080;">20</span> <span style="color:#000000;">        </span><span style="color:#0000FF;">var</span><span style="color:#000000;"> elem </span><span style="color:#000000;">=</span><span style="color:#000000;"> element.find(</span><span style="color:#000000;">&quot;</span><span style="color:#000000;">span</span><span style="color:#000000;">&quot;</span><span style="color:#000000;">);
</span><span style="color:#008080;">21</span> <span style="color:#000000;">        expect(elem[</span><span style="color:#000000;">0</span><span style="color:#000000;">].childNodes[</span><span style="color:#000000;">0</span><span style="color:#000000;">].data).toEqual(</span><span style="color:#000000;">&quot;</span><span style="color:#000000;">First Number</span><span style="color:#000000;">&quot;</span><span style="color:#000000;">);
</span><span style="color:#008080;">22</span> <span style="color:#000000;">    });
</span><span style="color:#008080;">23</span> <span style="color:#000000;">});</span></div></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>Line (4) &#8211; When testing directives, we will create an app and we load that every time before each test.</p>
<p>Line (6-11) are the ones required to generate the directive into HTML span element.</p>
<p>Line (6) – Will talk about compiling and linking little bit later, but for now, we inject rootScope and compile from angular mock to our test.</p>
<p>Line (7) – &lt;display-number&gt; is our directive under test. So we create an element with that directive as if it appears in the view.</p>
<p>Line (9) – Compile will walk through the HTML and create list of angular directives available.</p>
<p>Line (10) &#8211; $digest triggers the process converts the all the directives to proper HTML tag and generate associated watch etc.,</p>
<p>Line (13 – 16) is our first test to make sure there is span tag created after directive processed.</p>
<p>Line (18 – 22) is our second test, which make sure the text ‘First Number’ shows up in the Span.</p>
<p>If you would run, the specRunner.html, both the tests will fail. </p>
<p><strong>View with the directive.</strong></p>
<p>You can find the <a href="http://jsfiddle.net/unni/k89wn/">solution in jsFiddle</a>.</p>
<div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:11ba21ca-9b01-4a3f-aaf7-adb532adb8a5" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<pre style="width:859px;height:126px;background-color:White;overflow:auto;"><div><span style="color:#008080;">1</span> <span style="color:#0000FF;">&lt;</span><span style="color:#800000;">div  </span><span style="color:#FF0000;">ng-controller</span><span style="color:#0000FF;">=&quot;additionCtrl&quot;</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">2</span> <span style="color:#000000;">        </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">display-number</span><span style="color:#0000FF;">&gt;&lt;/</span><span style="color:#800000;">display-number</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">: </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">input </span><span style="color:#FF0000;">ng-model</span><span style="color:#0000FF;">=&quot;NumberOne&quot;</span><span style="color:#0000FF;">/&gt;&lt;</span><span style="color:#800000;">br</span><span style="color:#0000FF;">/&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">3</span> <span style="color:#000000;">        Second Number: </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">input </span><span style="color:#FF0000;">ng-model</span><span style="color:#0000FF;">=&quot;NumberTwo&quot;</span><span style="color:#0000FF;">/&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">4</span> <span style="color:#000000;">        </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">button </span><span style="color:#FF0000;">ng-click</span><span style="color:#0000FF;">=&quot;add(NumberOne, NumberTwo)&quot;</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">Add</span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">button</span><span style="color:#0000FF;">&gt;&lt;</span><span style="color:#800000;">br</span><span style="color:#0000FF;">/&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">5</span> <span style="color:#000000;">        Result: {{Result}}
</span><span style="color:#008080;">6</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">div</span><span style="color:#0000FF;">&gt;</span></div></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>Line (2), you will see a new HTML tag &lt;display-number&gt;, this is the part which makes Angular awesome. We literally created a new HTML tag for the purpose of our coding. Even though other frameworks allow you to do something similar, you can not create new HTML element like Angular does. Couple of things to remember;</p>
<ol>
<li>Make sure ng-app is there in the HTML tag pointing to app module.</li>
<li>Always make sure, when you create an directive element, always put closing element. Don’t use inline closing like &lt;display-number /&gt;</li>
</ol>
<p>Here is the <strong>controller with directive</strong></p>
<p><div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:f5724337-48aa-48af-9629-1dc86e0e5021" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<pre style="width:859px;height:307px;background-color:White;overflow:auto;"><div><span style="color:#008080;"> 1</span> <span style="color:#0000FF;">var</span><span style="color:#000000;"> app </span><span style="color:#000000;">=</span><span style="color:#000000;"> angular.module(</span><span style="color:#000000;">&quot;</span><span style="color:#000000;">additionApp</span><span style="color:#000000;">&quot;</span><span style="color:#000000;">, []);
</span><span style="color:#008080;"> 2</span> <span style="color:#000000;">
</span><span style="color:#008080;"> 3</span> <span style="color:#000000;">app.directive(</span><span style="color:#000000;">&quot;</span><span style="color:#000000;">displayNumber</span><span style="color:#000000;">&quot;</span><span style="color:#000000;">, </span><span style="color:#0000FF;">function</span><span style="color:#000000;"> () {
</span><span style="color:#008080;"> 4</span> <span style="color:#000000;">    </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> {
</span><span style="color:#008080;"> 5</span> <span style="color:#000000;">        restrict: </span><span style="color:#000000;">'</span><span style="color:#000000;">E</span><span style="color:#000000;">'</span><span style="color:#000000;">,
</span><span style="color:#008080;"> 6</span> <span style="color:#000000;">        template: </span><span style="color:#000000;">'</span><span style="color:#000000;">&lt;span&gt;First Number&lt;/span&gt;</span><span style="color:#000000;">'</span><span style="color:#000000;">
</span><span style="color:#008080;"> 7</span> <span style="color:#000000;">    };
</span><span style="color:#008080;"> 8</span> <span style="color:#000000;">});
</span><span style="color:#008080;"> 9</span> <span style="color:#000000;">
</span><span style="color:#008080;">10</span> <span style="color:#000000;"></span><span style="color:#0000FF;">function</span><span style="color:#000000;"> additionCtrl($scope) {
</span><span style="color:#008080;">11</span> <span style="color:#000000;">    $scope.NumberOne </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">0</span><span style="color:#000000;">;
</span><span style="color:#008080;">12</span> <span style="color:#000000;">    $scope.NumberTwo </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">0</span><span style="color:#000000;">;
</span><span style="color:#008080;">13</span> <span style="color:#000000;">    $scope.Result </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">0</span><span style="color:#000000;">;
</span><span style="color:#008080;">14</span> <span style="color:#000000;">    $scope.add </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#0000FF;">function</span><span style="color:#000000;"> (a, b) {
</span><span style="color:#008080;">15</span> <span style="color:#000000;">        $scope.Result </span><span style="color:#000000;">=</span><span style="color:#000000;"> Number(a) </span><span style="color:#000000;">+</span><span style="color:#000000;"> Number(b);
</span><span style="color:#008080;">16</span> <span style="color:#000000;">    };
</span><span style="color:#008080;">17</span> <span style="color:#000000;">}</span></div></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>Line (1) when using directives, similar to Filters, you need to have an app associated with it.</p>
<p>Line (3) sets up the directive. If you notice the method name is ‘displayNumber’ (camel case). But when you use the directive you use display-number. </p>
<p>Line (4) there are couple of ways, you can create directives and we are following a simple approach where it return a class for directives.</p>
<p>Line (5) ‘restrict’ specifies, is the directive an ‘Element’ (E), Attribute inside an element (A), Class (C) or Command (M). In our case we are creating a new element called display-number so we are restricting it to an Element. The default is Attribute.</p>
<p>Line (6) provides the template that need to be used in place of the directive. In this case, it is nothing but straight forward span element.</p>
<p>Rest of the code is our old controller. Next we will look at how to pass in values to </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/csharprambling.wordpress.com/500/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/csharprambling.wordpress.com/500/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=csharprambling.wordpress.com&#038;blog=18899480&#038;post=500&#038;subd=csharprambling&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://csharprambling.wordpress.com/2013/04/15/silverlight-to-angular-7-directives-i/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/11e06646d58e4efc69ef59a8da40a25c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ksunair</media:title>
		</media:content>
	</item>
		<item>
		<title>Silverlight to Angular &#8211; 6 (Unit Testing)</title>
		<link>http://csharprambling.wordpress.com/2013/04/12/silverlight-to-angular-6-unit-testing/</link>
		<comments>http://csharprambling.wordpress.com/2013/04/12/silverlight-to-angular-6-unit-testing/#comments</comments>
		<pubDate>Fri, 12 Apr 2013 20:30:00 +0000</pubDate>
		<dc:creator>Unni</dc:creator>
				<category><![CDATA[AngularJS]]></category>
		<category><![CDATA[Jasmine]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">https://csharprambling.wordpress.com/?p=498</guid>
		<description><![CDATA[Unit testing is one of the important and most often overlooked process in development. When we started development in Silverlight, we were so excited that with clear model, view and viewmodel separation, we were all set to create a fully &#8230; <a href="http://csharprambling.wordpress.com/2013/04/12/silverlight-to-angular-6-unit-testing/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=csharprambling.wordpress.com&#038;blog=18899480&#038;post=498&#038;subd=csharprambling&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Unit testing is one of the important and most often overlooked process in development. When we started development in Silverlight, we were so excited that with clear model, view and viewmodel separation, we were all set to create a fully tested code. Before you knew it, we created production code with Silverlight and shipped with 0% unit test. So the bottom line is, even if there are ways to create unit test, it is up to us to create unit tests. Well, creating unit tests is one thing and creating proper unit test is different thing. I remember creating unit tests, with reading data from database, calling bunch of methods to massage data before actually calling the method to test. I learned it hard way that, it was not right unit test. So there are two aspects of unit tests we need to remember, one is to write unit test and another is to write proper unit test. In this blog, lets look at the unit testing in Angular. Here is where I am going to break off from Silverlight and start to focus on Angular. One side note, because of the nature of Javascript when developing big application, do yourself a favor make sure, it have 100% coverage.</p>
<p>AngularJS has a decent but <a href="http://docs.angularjs.org/guide/dev_guide.unit-testing">incomplete document on unit testing</a>. Hopefully they will come around and finish it. We will look at straight forward unit testing with simple controller. If you have noticed so far, our controller is nothing but pure Javascript. It does not have any references to anything else like DOM manipulation or XFR calls. So our testing so far will be nothing but simple Javascript testing. It turned out there are ton of <a href="http://stackoverflow.com/questions/300855/looking-for-a-better-javascript-unit-test-tool">unit testing frameworks available for Javascript</a>. As a Javascript beginner myself, I was looking for some testing framework which looked familiar and finally I settled on <a href="http://pivotal.github.io/jasmine/">Jasmine</a>. I made this decision purely on my previous RSpec experience. Once I get a handle on that, I will explore the other options available. If you would follow <a href="http://pivotal.github.io/jasmine/">this link</a>, it has fantastic one page reference to all the features available in Jasmine. It should not take more than an hour to go through and try them all out.</p>
<p>Lets see how can we create unit test for one of our sample code.</p>
<p><strong>Problem: Given two numbers(integer or float, positive or negative), add them. </strong></p>
<p>Approach: As a developer, I am still learning to approach this problem with TDD in mind. So to do TDD, you first write out the test cases your function need to handle before writing the actual code. So what are the possible test cases?</p>
<ol>
<li>It should add two valid integer numbers.</li>
<li>It should add one integer and one float.</li>
<li>it should add two float numbers.</li>
<li>It should add when the numbers are zeros.</li>
<li>It should add when one number is zero.</li>
<li>It should add two negative numbers.</li>
<li>It should add when one number is negative number.</li>
<li>If first parameter is non numeric number, do not add.</li>
<li>If second parameter is non numeric, do not add.</li>
<li>If both the input are non numeric, do not add.</li>
</ol>
<p>The proper TDD is only to write unit test based on the requirements. If you look at the (8-10), it was not mentioned in the requirement then why would we need to handle that situation? So don’t write those test cases.</p>
<p>So based on our requirement, I came up with 7 (skipped 3) test cases. Following TDD, write test first, make it fail and then write the code to pass. I came across two different approaches as well. One was to write the first test, make it fail and then write the code to make it pass and then go to your next test. The second approach was to write all the tests first and write the code to make them pass. I like earlier approach than later one. As I said before, I am going to use Jasmine and you can follow the install instructions in Jasmine to get Jasmine going and then follow along here; Here is the directory structure of my application</p>
<p><a href="http://csharprambling.files.wordpress.com/2013/04/image2.png"><img title="image" style="border-top:0;border-right:0;border-bottom:0;border-left:0;display:inline;" border="0" alt="image" src="http://csharprambling.files.wordpress.com/2013/04/image_thumb2.png?w=224&#038;h=215" width="224" height="215" /></a> </p>
<p>Under main web site, I have two folders, one for the app code and another folder for the testing. This way, we will not mix the production code with testing code and will not distribute the test code to production either. The test folder has spec folder, this is where all the unit tests housed. In the root of the test folder we have SpecRunner.html (we will see that in a minute), which when we run, will run all all the tests specified in the specrunner.</p>
<p><strong>Iteration 1:</strong></p>
<p><strong>Unit Test:</strong></p>
<p>First we are going to write our test code and we call it addition.spec.js; and the code looks like the following to accommodate our first test; </p>
<div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:630698c1-d0d7-4be0-ae6d-d2cdb60882f4" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<pre style="width:859px;height:302px;background-color:White;overflow:auto;"><div><span style="color:#008080;"> 1</span> <span style="color:#000000;">describe(</span><span style="color:#000000;">&quot;</span><span style="color:#000000;">Addition</span><span style="color:#000000;">&quot;</span><span style="color:#000000;">, </span><span style="color:#0000FF;">function</span><span style="color:#000000;"> () {
</span><span style="color:#008080;"> 2</span> <span style="color:#000000;">
</span><span style="color:#008080;"> 3</span> <span style="color:#000000;">    </span><span style="color:#0000FF;">var</span><span style="color:#000000;"> $scope, ctrl;
</span><span style="color:#008080;"> 4</span> <span style="color:#000000;">
</span><span style="color:#008080;"> 5</span> <span style="color:#000000;">    beforeEach(inject(</span><span style="color:#0000FF;">function</span><span style="color:#000000;"> ($rootScope, $controller) {
</span><span style="color:#008080;"> 6</span> <span style="color:#000000;">        $scope </span><span style="color:#000000;">=</span><span style="color:#000000;"> $rootScope.$</span><span style="color:#0000FF;">new</span><span style="color:#000000;">();
</span><span style="color:#008080;"> 7</span> <span style="color:#000000;">        ctrl </span><span style="color:#000000;">=</span><span style="color:#000000;"> $controller(</span><span style="color:#000000;">'</span><span style="color:#000000;">additionCtrl</span><span style="color:#000000;">'</span><span style="color:#000000;">, {
</span><span style="color:#008080;"> 8</span> <span style="color:#000000;">            $scope: $scope
</span><span style="color:#008080;"> 9</span> <span style="color:#000000;">        });
</span><span style="color:#008080;">10</span> <span style="color:#000000;">    }));
</span><span style="color:#008080;">11</span> <span style="color:#000000;">
</span><span style="color:#008080;">12</span> <span style="color:#000000;">    it(</span><span style="color:#000000;">&quot;</span><span style="color:#000000;">should add two integer numbers.</span><span style="color:#000000;">&quot;</span><span style="color:#000000;">, </span><span style="color:#0000FF;">function</span><span style="color:#000000;"> () {
</span><span style="color:#008080;">13</span> <span style="color:#000000;">        $scope.add(</span><span style="color:#000000;">2</span><span style="color:#000000;">, </span><span style="color:#000000;">3</span><span style="color:#000000;">);
</span><span style="color:#008080;">14</span> <span style="color:#000000;">        expect($scope.Result).toEqual(</span><span style="color:#000000;">5</span><span style="color:#000000;">);
</span><span style="color:#008080;">15</span> <span style="color:#000000;">    });
</span><span style="color:#008080;">16</span> <span style="color:#000000;">});
</span><span style="color:#008080;">17</span> <span style="color:#000000;"></span></div></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
</p>
<p>Just to test 2 line of code we ended up writing close to 16 lines of code. This is because, we want to bring angular context and scope in to the testing as if it is running through angular. It is done with <em>angular-mock.js</em>, this will get added to specrunner.html in the next step. For now, lets look at the line (5-10). It is important to understand, how does the test run. When Jasmine runs, it will first look for any <strong><em>‘beforeEach’</em></strong> (line 5) and if found, in our case, there is one and it get executed before every test.&#160; A test is nothing but the one starts with<strong><em> ‘it’</em></strong>, like at line (12). </p>
<p>Right now in our application, we are using default application and module and we are only creating a controller. As you know, in our controller code, we pass $scope from angular context to the controller. So to test our code, we need create mock context and that is what the controller have to use. To achieve this, before each test code, we will call inject method and for the giving rootScope, we create a mock Angular scope and that is passed to the controller in test as the scope. For now, you can use this as a template for writing our test and accessing the scope variable inside the test.</p>
<p>Lets look at the test itself, removing all the angular part out of it, which you will repeat in all your test suit once anyway. Without angular part in it, the test by itself will look like the following;</p>
<div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:26a033f6-0881-45a4-86a1-9cad9ad2893c" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<pre style="width:859px;height:132px;background-color:White;overflow:auto;"><div><span style="color:#008080;">1</span> <span style="color:#000000;">describe(</span><span style="color:#000000;">&quot;</span><span style="color:#000000;">Addition</span><span style="color:#000000;">&quot;</span><span style="color:#000000;">, </span><span style="color:#0000FF;">function</span><span style="color:#000000;"> () {
</span><span style="color:#008080;">2</span> <span style="color:#000000;">    it(</span><span style="color:#000000;">&quot;</span><span style="color:#000000;">should add two integer numbers.</span><span style="color:#000000;">&quot;</span><span style="color:#000000;">, </span><span style="color:#0000FF;">function</span><span style="color:#000000;"> () {
</span><span style="color:#008080;">3</span> <span style="color:#000000;">        $scope.add(</span><span style="color:#000000;">2</span><span style="color:#000000;">, </span><span style="color:#000000;">3</span><span style="color:#000000;">);
</span><span style="color:#008080;">4</span> <span style="color:#000000;">        expect($scope.Result).toEqual(</span><span style="color:#000000;">5</span><span style="color:#000000;">);
</span><span style="color:#008080;">5</span> <span style="color:#000000;">    });
</span><span style="color:#008080;">6</span> <span style="color:#000000;">});</span></div></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>So here is the rundown of this test;</p>
<ol>
<li>All the test starts with a test suite with ‘describe’ (line 1). Each test suite is given a suit name (Addition, in our case).</li>
<li>If each test needs some common things to happen, like initializing, then call ‘beforeEach’ (line 5 in the previous complete test code).</li>
<li>Every test, starts with ‘it’. ‘it’ has two parts (line 2), one is the name of the test and second one the test itself. </li>
<li>In every test, after some action, validate the test result by ‘expect’ (line 4). ‘expect’ also has three parts, source, target and comparison operator. In our example, source is $scope.Result, target is ‘5’ and comparison operator is ‘toEqual’.</li>
</ol>
<p>I would strongly recommend anyone interested in testing with Jasmine to read through their <a href="http://pivotal.github.io/jasmine/">fantastic documentation with example</a>.</p>
<p><strong>Jasmine Spec Runner:</strong></p>
<p>Now that the first test unit test code completed, lets add the information required to run the unit test in Jasmine. We run Specrunner.html to the tests we want to run. </p>
<div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:e8fdb1a8-3b63-4f07-bdb5-cb7548c5d859" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<pre style="width:859px;height:437px;background-color:White;overflow:auto;"><div><span style="color:#008080;"> 1</span> <span style="color:#0000FF;">&lt;!</span><span style="color:#FF00FF;">DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;
</span><span style="color:#008080;"> 2</span> <span style="color:#FF00FF;">  &quot;http://www.w3.org/TR/html4/loose.dtd&quot;</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 3</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">html</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 4</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">head</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 5</span> <span style="color:#000000;">  </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">title</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">Jasmine Spec Runner</span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">title</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 6</span> <span style="color:#000000;">    
</span><span style="color:#008080;"> 7</span> <span style="color:#000000;">  </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">link </span><span style="color:#FF0000;">rel</span><span style="color:#0000FF;">=&quot;shortcut icon&quot;</span><span style="color:#FF0000;"> type</span><span style="color:#0000FF;">=&quot;image/png&quot;</span><span style="color:#FF0000;"> href</span><span style="color:#0000FF;">=&quot;http://searls.github.com/jasmine-all/jasmine_favicon.png&quot;</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 8</span> <span style="color:#000000;">  </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">link </span><span style="color:#FF0000;">rel</span><span style="color:#0000FF;">=&quot;stylesheet&quot;</span><span style="color:#FF0000;"> type</span><span style="color:#0000FF;">=&quot;text/css&quot;</span><span style="color:#FF0000;"> href</span><span style="color:#0000FF;">=&quot;http://searls.github.com/jasmine-all//jasmine.css&quot;</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 9</span> <span style="color:#000000;">  </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">script </span><span style="color:#FF0000;">type</span><span style="color:#0000FF;">=&quot;text/javascript&quot;</span><span style="color:#FF0000;"> src</span><span style="color:#0000FF;">=&quot;http://searls.github.com/jasmine-all/jasmine-all-min.js&quot;</span><span style="color:#0000FF;">&gt;&lt;/</span><span style="color:#800000;">script</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">10</span> <span style="color:#000000;">
</span><span style="color:#008080;">11</span> <span style="color:#000000;">    </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">script </span><span style="color:#FF0000;">type</span><span style="color:#0000FF;">=&quot;text/javascript&quot;</span><span style="color:#FF0000;"> src</span><span style="color:#0000FF;">=&quot;http://code.angularjs.org/1.0.6/angular.js&quot;</span><span style="color:#0000FF;">&gt;&lt;/</span><span style="color:#800000;">script</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">12</span> <span style="color:#000000;">    </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">script </span><span style="color:#FF0000;">type</span><span style="color:#0000FF;">=&quot;text/javascript&quot;</span><span style="color:#FF0000;"> src</span><span style="color:#0000FF;">=&quot;http://code.angularjs.org/1.0.6/angular-mocks.js&quot;</span><span style="color:#0000FF;">&gt;&lt;/</span><span style="color:#800000;">script</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">13</span> <span style="color:#000000;">
</span><span style="color:#008080;">14</span> <span style="color:#000000;">  </span><span style="color:#008000;">&lt;!--</span><span style="color:#008000;"> include source files here... </span><span style="color:#008000;">--&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">15</span> <span style="color:#000000;">  </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">script </span><span style="color:#FF0000;">type</span><span style="color:#0000FF;">=&quot;text/javascript&quot;</span><span style="color:#FF0000;"> src</span><span style="color:#0000FF;">=&quot;../App/Scripts/additionCtrl.js&quot;</span><span style="color:#0000FF;">&gt;&lt;/</span><span style="color:#800000;">script</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">16</span> <span style="color:#000000;">
</span><span style="color:#008080;">17</span> <span style="color:#000000;">  </span><span style="color:#008000;">&lt;!--</span><span style="color:#008000;"> include spec files here... </span><span style="color:#008000;">--&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">18</span> <span style="color:#000000;">  </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">script </span><span style="color:#FF0000;">type</span><span style="color:#0000FF;">=&quot;text/javascript&quot;</span><span style="color:#FF0000;"> src</span><span style="color:#0000FF;">=&quot;spec/addition.spec.js&quot;</span><span style="color:#0000FF;">&gt;&lt;/</span><span style="color:#800000;">script</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">    
</span><span style="color:#008080;">19</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">head</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">20</span> <span style="color:#000000;">
</span><span style="color:#008080;">21</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">body</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">22</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">body</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">23</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">html</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">24</span> <span style="color:#000000;"></span></div></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>Line (6 – 12) are all the required javascripts. Based on your application, you will add more library scripts here.</p>
<p>Line (15) specified which is the controller under test. This is pointing to the production code under ‘App’ folder.</p>
<p>Line (18) specifies, which are all the unit tests to run. In our case we have only one unit test spec to run.</p>
<p><strong>App Setup:</strong></p>
<p>You can run the test as it is now and it will fail with error like unable to find additionalCtrl etc., that is our red to green approach anyway.</p>
<p>Now that all the hooks are in place, open the windows explorer, find Specrunner.html and double click it. It will open a browser and run the test. This test will fail since we did not add any controller or view. So lets add them.</p>
<p>view:</p>
<div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:59776b98-2b13-4c32-b51e-5b8af1fe01b9" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<pre style="width:859px;height:298px;background-color:White;overflow:auto;"><div><span style="color:#008080;"> 1</span> <span style="color:#0000FF;">&lt;!</span><span style="color:#FF00FF;">DOCTYPE html</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 2</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">html </span><span style="color:#FF0000;">ng-app</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 3</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">head</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 4</span> <span style="color:#000000;">    </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">title</span><span style="color:#0000FF;">&gt;&lt;/</span><span style="color:#800000;">title</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 5</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">head</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 6</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">body</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 7</span> <span style="color:#000000;">    </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">div </span><span style="color:#FF0000;">ng-controller</span><span style="color:#0000FF;">=&quot;additionCtrl&quot;</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 8</span> <span style="color:#000000;">        First Number: </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">input </span><span style="color:#FF0000;">ng-model</span><span style="color:#0000FF;">=&quot;NumberOne&quot;</span><span style="color:#0000FF;">/&gt;&lt;</span><span style="color:#800000;">br</span><span style="color:#0000FF;">/&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 9</span> <span style="color:#000000;">        Second Number: </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">input </span><span style="color:#FF0000;">ng-model</span><span style="color:#0000FF;">=&quot;NumberTwo&quot;</span><span style="color:#0000FF;">/&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">10</span> <span style="color:#000000;">        </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">button </span><span style="color:#FF0000;">ng-click</span><span style="color:#0000FF;">=&quot;add(NumberOne, NumberTwo)&quot;</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">Add</span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">button</span><span style="color:#0000FF;">&gt;&lt;</span><span style="color:#800000;">br</span><span style="color:#0000FF;">/&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">11</span> <span style="color:#000000;">        Result: {{Result}}
</span><span style="color:#008080;">12</span> <span style="color:#000000;">    </span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">div</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">13</span> <span style="color:#000000;">    </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">script </span><span style="color:#FF0000;">src</span><span style="color:#0000FF;">=&quot;https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js&quot;</span><span style="color:#0000FF;">&gt;&lt;/</span><span style="color:#800000;">script</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">14</span> <span style="color:#000000;">    </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">script </span><span style="color:#FF0000;">src</span><span style="color:#0000FF;">=&quot;Scripts/additionCtrl.js&quot;</span><span style="color:#0000FF;">&gt;&lt;/</span><span style="color:#800000;">script</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">15</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">body</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">16</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">html</span><span style="color:#0000FF;">&gt;</span></div></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>This is nothing more than vie, which we have seen before. </p>
<p>Controller:</p>
<p>Since we are following TDD, lets try make sure we stick to the minimum code to make the test pass.</p>
<p><div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:ae21c334-779c-45b6-a9eb-6c2fe34e74e9" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<pre style="width:859px;height:152px;background-color:White;overflow:auto;"><div><span style="color:#008080;">1</span> <span style="color:#0000FF;">function</span><span style="color:#000000;"> additionCtrl($scope) {
</span><span style="color:#008080;">2</span> <span style="color:#000000;">    $scope.NumberOne </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">0</span><span style="color:#000000;">;
</span><span style="color:#008080;">3</span> <span style="color:#000000;">    $scope.NumberTwo </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">0</span><span style="color:#000000;">;
</span><span style="color:#008080;">4</span> <span style="color:#000000;">    $scope.Result </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">0</span><span style="color:#000000;">;
</span><span style="color:#008080;">5</span> <span style="color:#000000;">    $scope.add </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#0000FF;">function</span><span style="color:#000000;"> (a, b) {
</span><span style="color:#008080;">6</span> <span style="color:#000000;">        $scope.Result </span><span style="color:#000000;">=</span><span style="color:#000000;"> parseInt(a) </span><span style="color:#000000;">+</span><span style="color:#000000;"> parseInt(b);
</span><span style="color:#008080;">7</span> <span style="color:#000000;">    };
</span><span style="color:#008080;">8</span> <span style="color:#000000;">}</span></div></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>This code takes two objects and convert it to int and then add them. As you would expect nothing special and still javascript function. Now if you would run the test should run successfully.</p>
<p>Now that our first test is successful, lets write the second test.</p>
<p>Iteration 2:</p>
<p>It should add one integer and one float. Add the following code to the addition.spec.js</p>
<div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:558f6a88-8b86-4c8d-96d2-dcde6ce2cf15" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<pre style="width:859px;height:84px;background-color:White;overflow:auto;"><div><span style="color:#008080;">1</span> <span style="color:#000000;">    it(</span><span style="color:#000000;">&quot;</span><span style="color:#000000;">should add one integer and one float numbers.</span><span style="color:#000000;">&quot;</span><span style="color:#000000;">, </span><span style="color:#0000FF;">function</span><span style="color:#000000;"> () {
</span><span style="color:#008080;">2</span> <span style="color:#000000;">        $scope.add(</span><span style="color:#000000;">2</span><span style="color:#000000;">, </span><span style="color:#000000;">3.5</span><span style="color:#000000;">);
</span><span style="color:#008080;">3</span> <span style="color:#000000;">        expect($scope.Result).toEqual(</span><span style="color:#000000;">5.5</span><span style="color:#000000;">);
</span><span style="color:#008080;">4</span> <span style="color:#000000;">    }); </span></div></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>When this test is run, we would expect it to pass but it will fail since we are doing parseInt, which is converting the number to int thus losing the fraction part. So now the test is RED, go back to and fix the code to make it run. Instead of using parseInt, use Number function, which will retain the fraction. </p>
<p>Here is the modified controller</p>
<div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:29966b49-ecbb-416b-97f1-7190d5b4ae3a" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<pre style="width:859px;height:153px;background-color:White;overflow:auto;"><div><span style="color:#008080;">1</span> <span style="color:#0000FF;">function</span><span style="color:#000000;"> additionCtrl($scope) {
</span><span style="color:#008080;">2</span> <span style="color:#000000;">    $scope.NumberOne </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">0</span><span style="color:#000000;">;
</span><span style="color:#008080;">3</span> <span style="color:#000000;">    $scope.NumberTwo </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">0</span><span style="color:#000000;">;
</span><span style="color:#008080;">4</span> <span style="color:#000000;">    $scope.Result </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">0</span><span style="color:#000000;">;
</span><span style="color:#008080;">5</span> <span style="color:#000000;">    $scope.add </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#0000FF;">function</span><span style="color:#000000;"> (a, b) {
</span><span style="color:#008080;">6</span> <span style="color:#000000;">        $scope.Result </span><span style="color:#000000;">=</span><span style="color:#000000;"> Number(a) </span><span style="color:#000000;">+</span><span style="color:#000000;"> Number(b);
</span><span style="color:#008080;">7</span> <span style="color:#000000;">    };
</span><span style="color:#008080;">8</span> <span style="color:#000000;">}</span></div></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>Now if you run specrunner.html the test pass. You keep adding test and make sure all test pass. </p>
<p>While learning unit testing with Angular, I ran into bunch of problems, thanks to <a href="http://stackoverflow.com/questions/15957191/why-do-i-get-inject-undefined-error-when-using-jasmine-with-angular">@karlgold</a> for helping me sort things out.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/csharprambling.wordpress.com/498/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/csharprambling.wordpress.com/498/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=csharprambling.wordpress.com&#038;blog=18899480&#038;post=498&#038;subd=csharprambling&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://csharprambling.wordpress.com/2013/04/12/silverlight-to-angular-6-unit-testing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/11e06646d58e4efc69ef59a8da40a25c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ksunair</media:title>
		</media:content>

		<media:content url="http://csharprambling.files.wordpress.com/2013/04/image_thumb2.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
		<item>
		<title>Silverlight to Angular &#8211; 5 (IValueConverter &#8211; filter)</title>
		<link>http://csharprambling.wordpress.com/2013/04/09/silverlight-to-angular-5-ivalueconverter-filter/</link>
		<comments>http://csharprambling.wordpress.com/2013/04/09/silverlight-to-angular-5-ivalueconverter-filter/#comments</comments>
		<pubDate>Tue, 09 Apr 2013 18:03:00 +0000</pubDate>
		<dc:creator>Unni</dc:creator>
				<category><![CDATA[AngularJS]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[filter]]></category>
		<category><![CDATA[module]]></category>
		<category><![CDATA[Silvelright]]></category>

		<guid isPermaLink="false">https://csharprambling.wordpress.com/?p=495</guid>
		<description><![CDATA[IValueConverter is without doubt one of the most used feature (at least in our side of the world) in Silverlight. We have lot of data, which, if we show them as it is to the user, it is completely useless. &#8230; <a href="http://csharprambling.wordpress.com/2013/04/09/silverlight-to-angular-5-ivalueconverter-filter/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=csharprambling.wordpress.com&#038;blog=18899480&#038;post=495&#038;subd=csharprambling&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>IValueConverter is without doubt one of the most used feature (at least in our side of the world) in Silverlight. We have lot of data, which, if we show them as it is to the user, it is completely useless. To make it more meaningful, we use Value Converter to convert and display it to the user. <a href="http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter.aspx">IValueConvert</a> provides a way to add logic to the binding data without changing the data. <em>IValueConverter</em> has two methods, <strong><em>‘Convert’</em></strong> to convert the original data that is bound but translate with some logic to give different result that then get rendered in the UI. <strong>‘ConvertBack’</strong> is to take user input and apply some logic to revert it to some other form to store it at the back end. Most of the time, whenever we use IValueConverter, we always end up using the <strong>‘Convert’</strong> and seldom use <strong>‘ConvertBack’, </strong>now I am going to use that as an excuse since Angular provides only one way convert, it does not have provision to convert it back. Even though I say, we don’t use ‘convertback’ but it is very useful feature and is used a lot, one good example is ‘Color Picker’. We will try to explore convert back at a later time. For now, we will see what is available in Angular out of the box that we can use.</p>
<p><strong>Problem: Multiply two numbers and display the result with two decimal points.</strong></p>
<p><strong>Silverlight:</strong></p>
<p>Only difference between <a href="http://csharprambling.wordpress.com/2013/04/07/silverlight-to-angular-4-datatemplates/">our previous solution</a> to this is that we have value converter and it is used in the binding to change the incoming data to more meaningful data. Lets look at the view</p>
<p><strong>View:</strong></p>
<div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:4af026c1-3061-4770-9ca8-eb20e54aa37c" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<pre style="width:859px;height:175px;background-color:White;overflow:auto;"><div><span style="color:#008080;">1</span> <span style="color:#000000;">        </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">Grid.Resources</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">2</span> <span style="color:#000000;">            </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">converter:TwoDigitConverter </span><span style="color:#FF0000;">x:Key</span><span style="color:#0000FF;">=&quot;Converter&quot;</span><span style="color:#FF0000;"> </span><span style="color:#0000FF;">/&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">3</span> <span style="color:#000000;">        </span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">Grid.Resources</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">4</span> <span style="color:#000000;">        </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">TextBlock</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">Number 1</span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">TextBlock</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">5</span> <span style="color:#000000;">        </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">TextBox </span><span style="color:#FF0000;">Text</span><span style="color:#0000FF;">=&quot;</span><span style="color:#808000;">{Binding NumberOne, Mode=TwoWay}</span><span style="color:#0000FF;">&quot;</span><span style="color:#FF0000;"> Grid.Column</span><span style="color:#0000FF;">=&quot;1&quot;</span><span style="color:#0000FF;">&gt;&lt;/</span><span style="color:#800000;">TextBox</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">6</span> <span style="color:#000000;">        </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">TextBlock </span><span style="color:#FF0000;">Grid.Row</span><span style="color:#0000FF;">=&quot;1&quot;</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">Number 2</span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">TextBlock</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">7</span> <span style="color:#000000;">        </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">TextBox </span><span style="color:#FF0000;">Text</span><span style="color:#0000FF;">=&quot;</span><span style="color:#808000;">{Binding NumberTwo, Mode=TwoWay}</span><span style="color:#0000FF;">&quot;</span><span style="color:#FF0000;"> Grid.Row</span><span style="color:#0000FF;">=&quot;1&quot;</span><span style="color:#FF0000;"> Grid.Column</span><span style="color:#0000FF;">=&quot;1&quot;</span><span style="color:#0000FF;">&gt;&lt;/</span><span style="color:#800000;">TextBox</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">8</span> <span style="color:#000000;">        </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">Button </span><span style="color:#FF0000;">Grid.Row</span><span style="color:#0000FF;">=&quot;1&quot;</span><span style="color:#FF0000;"> Grid.Column</span><span style="color:#0000FF;">=&quot;2&quot;</span><span style="color:#FF0000;"> Command</span><span style="color:#0000FF;">=&quot;</span><span style="color:#808000;">{Binding Multiply}</span><span style="color:#0000FF;">&quot;</span><span style="color:#FF0000;"> CommandParameter</span><span style="color:#0000FF;">=&quot;&quot;</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">Multiply</span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">Button</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">9</span> <span style="color:#000000;">        </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">TextBlock </span><span style="color:#FF0000;">Grid.Row</span><span style="color:#0000FF;">=&quot;2&quot;</span><span style="color:#FF0000;"> Grid.Column</span><span style="color:#0000FF;">=&quot;1&quot;</span><span style="color:#FF0000;"> Text</span><span style="color:#0000FF;">=&quot;</span><span style="color:#808000;">{Binding Total, Converter={StaticResource Converter}}</span><span style="color:#0000FF;">&quot;</span><span style="color:#0000FF;">&gt;&lt;/</span><span style="color:#800000;">TextBlock</span><span style="color:#0000FF;">&gt;</span></div></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>Line (2 &#8211; 4) provides us the hook into the value converter to be used in the XAML. Line (9) binds the result, even though the it binds to the result, we are asking the binding to use the associated converter to apply the conversion based on the convert logic and use the result to display in the ‘TextBlock’.</p>
<p><strong>ValueConverter:</strong></p>
<div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:510c547f-cb4c-44a6-84ef-6697d7ffeca3" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<pre style="width:859px;height:278px;background-color:White;overflow:auto;"><div><span style="color:#008080;"> 1</span> <span style="color:#000000;">    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">class</span><span style="color:#000000;"> TwoDigitConverter:IValueConverter
</span><span style="color:#008080;"> 2</span> <span style="color:#000000;">    {
</span><span style="color:#008080;"> 3</span> <span style="color:#000000;">
</span><span style="color:#008080;"> 4</span> <span style="color:#000000;">        </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">object</span><span style="color:#000000;"> Convert(</span><span style="color:#0000FF;">object</span><span style="color:#000000;"> value, Type targetType, </span><span style="color:#0000FF;">object</span><span style="color:#000000;"> parameter, System.Globalization.CultureInfo culture)
</span><span style="color:#008080;"> 5</span> <span style="color:#000000;">        {
</span><span style="color:#008080;"> 6</span> <span style="color:#000000;">            var result </span><span style="color:#000000;">=</span><span style="color:#000000;"> value </span><span style="color:#0000FF;">as</span><span style="color:#000000;"> </span><span style="color:#0000FF;">float</span><span style="color:#000000;">?</span><span style="color:#000000;">;
</span><span style="color:#008080;"> 7</span> <span style="color:#000000;">            result </span><span style="color:#000000;">=</span><span style="color:#000000;"> result </span><span style="color:#000000;">??</span><span style="color:#000000;"> </span><span style="color:#800080;">0</span><span style="color:#000000;">;
</span><span style="color:#008080;"> 8</span> <span style="color:#000000;">            </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> </span><span style="color:#0000FF;">string</span><span style="color:#000000;">.Format(</span><span style="color:#800000;">&quot;</span><span style="color:#800000;">{0:N2}</span><span style="color:#800000;">&quot;</span><span style="color:#000000;">, result);
</span><span style="color:#008080;"> 9</span> <span style="color:#000000;">        }
</span><span style="color:#008080;">10</span> <span style="color:#000000;">
</span><span style="color:#008080;">11</span> <span style="color:#000000;">        </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">object</span><span style="color:#000000;"> ConvertBack(</span><span style="color:#0000FF;">object</span><span style="color:#000000;"> value, Type targetType, </span><span style="color:#0000FF;">object</span><span style="color:#000000;"> parameter, System.Globalization.CultureInfo culture)
</span><span style="color:#008080;">12</span> <span style="color:#000000;">        {
</span><span style="color:#008080;">13</span> <span style="color:#000000;">            </span><span style="color:#0000FF;">throw</span><span style="color:#000000;"> </span><span style="color:#0000FF;">new</span><span style="color:#000000;"> NotImplementedException();
</span><span style="color:#008080;">14</span> <span style="color:#000000;">        }
</span><span style="color:#008080;">15</span> <span style="color:#000000;">    }</span></div></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>When we create Value Converter, it has to implement IValueConverter, which have only two methods that we need to implement. Convert and ConvertBack as we discussed in the beginning. Since Angular only supports one way conversion, we created this sample conveniently to show case only the ‘Convert’ part of ValueConverter.&#160; Convert method does not do anything special, it takes the input number and returns the result with 2 decimal digits.</p>
<p>Our view model exposes the three variables and a command, nothing else.</p>
<p><strong>Angular:</strong></p>
<p>Angular provides filters, a way to format data for display. There are couple of ways we can use filters. You can use filters<em> in line</em> or create <em>custom filters. </em>If you were to create custom filters, you can reuse them in any controllers in a given module, it is like using resource file in Silverlight. One additional benefit of the creating custom filters, you can chain them to pass the input to multiple functions before creating final result. Lets first look at the inline approach to solve the problem and then we will write custom filter.</p>
<p><strong>In-Line formatting:</strong></p>
<p><strong>View:</strong></p>
<p>Since it is inline formatting, the formatting happens in the View code itself.</p>
<div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:658234fb-060b-4c3f-8b00-cb862020066f" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<pre style="width:859px;height:309px;background-color:White;overflow:auto;"><div><span style="color:#008080;"> 1</span> <span style="color:#0000FF;">&lt;!</span><span style="color:#FF00FF;">DOCTYPE html</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 2</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">html </span><span style="color:#FF0000;">ng-app</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 3</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">head</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 4</span> <span style="color:#000000;">    </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">title</span><span style="color:#0000FF;">&gt;&lt;/</span><span style="color:#800000;">title</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 5</span> <span style="color:#000000;">    </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">script </span><span style="color:#FF0000;">src</span><span style="color:#0000FF;">=&quot;https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js&quot;</span><span style="color:#0000FF;">&gt;&lt;/</span><span style="color:#800000;">script</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 6</span> <span style="color:#000000;">    </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">script </span><span style="color:#FF0000;">src</span><span style="color:#0000FF;">=&quot;scripts/controller.js&quot;</span><span style="color:#0000FF;">&gt;&lt;/</span><span style="color:#800000;">script</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 7</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">head</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 8</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">body</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 9</span> <span style="color:#000000;">    </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">div </span><span style="color:#FF0000;">ng-controller</span><span style="color:#0000FF;">=&quot;controller&quot;</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">10</span> <span style="color:#000000;">        First Number: </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">input </span><span style="color:#FF0000;">ng-model</span><span style="color:#0000FF;">=&quot;NumberOne&quot;</span><span style="color:#0000FF;">/&gt;&lt;</span><span style="color:#800000;">br</span><span style="color:#0000FF;">/&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">11</span> <span style="color:#000000;">        Second Number: </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">input </span><span style="color:#FF0000;">ng-model</span><span style="color:#0000FF;">=&quot;NumberTwo&quot;</span><span style="color:#0000FF;">/&gt;&lt;</span><span style="color:#800000;">button </span><span style="color:#FF0000;">ng-click</span><span style="color:#0000FF;">=&quot;Multiply()&quot;</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">Multiply</span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">button</span><span style="color:#0000FF;">&gt;&lt;</span><span style="color:#800000;">br</span><span style="color:#0000FF;">/&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">12</span> <span style="color:#000000;">        Result : {{Result | number:2}}
</span><span style="color:#008080;">13</span> <span style="color:#000000;">    </span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">div</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">14</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">body</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">15</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">html</span><span style="color:#0000FF;">&gt;</span></div></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>Everything is standard here except line (12). In line 12, we get the result from the controller and angular will read the result and feed it to the expression in the pipe. In this case we are using number filter out of box to format number. There are few filters available out of box,&#160; take a look at this <a href="http://www.cheatography.com/proloser/cheat-sheets/angularjs/">AngularJS cheat sheet</a>.</p>
<p><strong>Custom formatting:</strong></p>
<p>There are situations, where out of the box filters will not be sufficient and we will need some custom filters to format the data. Angular filters are very powerful on that regard. As I said previously, </p>
<ol>
<li>Since you can create filters as part of module, these filters are available across all the controllers in the module.</li>
<li>Filters can be chained to create more customizable output data.</li>
<li>Compare to Silverlight, we can pass in as many parameters as you want to the filters.</li>
<li>If the filters are very simple and out of box, use the default filters.</li>
<li>It written properly, you can call filters with only available arguments and rest can assume default values(like optional parameters in c#).</li>
</ol>
<p>So in our case, even though we could use simple out of the box formatting, we will create a custom filter just for the sake of demonstration. So far in all our examples we left <strong>ng-app as empty string </strong>but when you want to use filters then you need to create a module and assign the filter to that module. With that in mind, here is the modified view.</p>
<div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:094a902c-61ec-4491-a82f-7fb3d7ab23c4" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<pre style="width:859px;height:309px;background-color:White;overflow:auto;"><div><span style="color:#008080;"> 1</span> <span style="color:#0000FF;">&lt;!</span><span style="color:#FF00FF;">DOCTYPE html</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 2</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">html </span><span style="color:#FF0000;">ng-app</span><span style="color:#0000FF;">='customFilters'</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 3</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">head</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 4</span> <span style="color:#000000;">    </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">title</span><span style="color:#0000FF;">&gt;&lt;/</span><span style="color:#800000;">title</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 5</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">head</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 6</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">body</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 7</span> <span style="color:#000000;">    </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">div </span><span style="color:#FF0000;">ng-controller</span><span style="color:#0000FF;">=&quot;controller&quot;</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 8</span> <span style="color:#000000;">        First Number: </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">input </span><span style="color:#FF0000;">ng-model</span><span style="color:#0000FF;">=&quot;NumberOne&quot;</span><span style="color:#0000FF;">/&gt;&lt;</span><span style="color:#800000;">br</span><span style="color:#0000FF;">/&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 9</span> <span style="color:#000000;">        Second Number: </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">input </span><span style="color:#FF0000;">ng-model</span><span style="color:#0000FF;">=&quot;NumberTwo&quot;</span><span style="color:#0000FF;">/&gt;&lt;</span><span style="color:#800000;">button </span><span style="color:#FF0000;">ng-click</span><span style="color:#0000FF;">=&quot;Multiply()&quot;</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">Multiply</span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">button</span><span style="color:#0000FF;">&gt;&lt;</span><span style="color:#800000;">br</span><span style="color:#0000FF;">/&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">10</span> <span style="color:#000000;">        Result : {{Result | twoDigits}}
</span><span style="color:#008080;">11</span> <span style="color:#000000;">    </span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">div</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">12</span> <span style="color:#000000;">    </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">script </span><span style="color:#FF0000;">src</span><span style="color:#0000FF;">=&quot;https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js&quot;</span><span style="color:#0000FF;">&gt;&lt;/</span><span style="color:#800000;">script</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">13</span> <span style="color:#000000;">    </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">script </span><span style="color:#FF0000;">src</span><span style="color:#0000FF;">=&quot;scripts/controller.js&quot;</span><span style="color:#0000FF;">&gt;&lt;/</span><span style="color:#800000;">script</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">14</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">body</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">15</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">html</span><span style="color:#0000FF;">&gt;</span></div></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p><strong><em>Line (2) now has the module name.</em></strong> We named it as ‘customFilters’. You will see in the view model, how we create a module and add the filters into the module.</p>
<p><em>**Note: It important that you put the module name in ng-app, if not none of the custom filters you write will work. Take it from me, I spend hours trying to figure out why my custom filter was not working, finally to find out, I missed the module name in the ng-app. **</em></p>
<p>Line (10) now feeds the result value to the custom filter ‘twoDigits’, which will do the magic of converting the result to a number with two decimal digits.</p>
<p><strong>Controller:</strong></p>
<div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:3ac0f5fb-1f83-466d-95fd-040bc0bcad70" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<pre style="width:859px;height:309px;background-color:White;overflow:auto;"><div><span style="color:#008080;"> 1</span> <span style="color:#000000;">angular.module(</span><span style="color:#000000;">'</span><span style="color:#000000;">customFilters</span><span style="color:#000000;">'</span><span style="color:#000000;">, []).
</span><span style="color:#008080;"> 2</span> <span style="color:#000000;">    filter(</span><span style="color:#000000;">'</span><span style="color:#000000;">twoDigits</span><span style="color:#000000;">'</span><span style="color:#000000;">, </span><span style="color:#0000FF;">function</span><span style="color:#000000;">() {
</span><span style="color:#008080;"> 3</span> <span style="color:#000000;">        </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> </span><span style="color:#0000FF;">function</span><span style="color:#000000;">(input) {
</span><span style="color:#008080;"> 4</span> <span style="color:#000000;">            </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> input.toFixed(</span><span style="color:#000000;">2</span><span style="color:#000000;">);
</span><span style="color:#008080;"> 5</span> <span style="color:#000000;">        };
</span><span style="color:#008080;"> 6</span> <span style="color:#000000;">    });
</span><span style="color:#008080;"> 7</span> <span style="color:#000000;">
</span><span style="color:#008080;"> 8</span> <span style="color:#000000;"></span><span style="color:#0000FF;">function</span><span style="color:#000000;"> controller($scope) {
</span><span style="color:#008080;"> 9</span> <span style="color:#000000;">    $scope.NumberOne </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">0</span><span style="color:#000000;">;
</span><span style="color:#008080;">10</span> <span style="color:#000000;">    $scope.NumberTwo </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">0</span><span style="color:#000000;">;
</span><span style="color:#008080;">11</span> <span style="color:#000000;">    $scope.Result </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">0</span><span style="color:#000000;">;
</span><span style="color:#008080;">12</span> <span style="color:#000000;">    $scope.Multiply </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#0000FF;">function</span><span style="color:#000000;"> () {
</span><span style="color:#008080;">13</span> <span style="color:#000000;">        $scope.Result </span><span style="color:#000000;">=</span><span style="color:#000000;"> $scope.NumberOne </span><span style="color:#000000;">*</span><span style="color:#000000;"> $scope.NumberTwo;
</span><span style="color:#008080;">14</span> <span style="color:#000000;">    };
</span><span style="color:#008080;">15</span> <span style="color:#000000;">}</span></div></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>So what is new compare to what we have seen so far?&#160; Line (1-6) creates the module and adds the custom filter to the module.</p>
<p>Line (1) – creates the module named ‘customFilters’. During the angular context generation, angular get hold of ng-app for the first time, it will look for the definition of module in the controller to bootstraps the application. In the module definition, there are two parameters, first one is the name of the module and for sake of simplicity, lets ignore the second parameter ‘[]’ for now.</p>
<p>Line (2) – Please note that, there is a <strong>‘.’ </strong>at the end of the line (1), with that we are directly adding a filter called ‘twoDigits’ to the filter factory. </p>
<p>Line (3) – Actual function which takes input and perform some task, in here format number to two decimal digits (line 4) and return the result.</p>
<p>Even though I presented ‘filters’ as way to format data, it is much more than that, it can do what ‘filter’ is supposed to do like <strong>filtering</strong>. We will look at that in one of the later post. Lets take this example one more level up to show how to pass parameters to the filter.</p>
<p><strong>Problem: Two two number and multiple and set the result precession based on user input.</strong></p>
<p><strong>View:</strong> </p>
<p>The view is exactly same as the last one except now, we have one input field to enter number of decimal digits user wants to see in the result.</p>
<div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:ab0d64e9-4e6f-4a22-a3cb-edccd36c180b" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<pre style="width:859px;height:176px;background-color:White;overflow:auto;"><div><span style="color:#008080;">1</span> <span style="color:#0000FF;">&lt;</span><span style="color:#800000;">div </span><span style="color:#FF0000;">ng:app</span><span style="color:#0000FF;">=&quot;customFilters&quot;</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">2</span> <span style="color:#000000;"> </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">div </span><span style="color:#FF0000;">ng-controller</span><span style="color:#0000FF;">=&quot;filterCntl&quot;</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">3</span> <span style="color:#000000;">     First Number: </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">input </span><span style="color:#FF0000;">ng-model</span><span style="color:#0000FF;">='NumberOne'</span><span style="color:#0000FF;">&gt;&lt;/</span><span style="color:#800000;">input</span><span style="color:#0000FF;">&gt;&lt;</span><span style="color:#800000;">br</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">4</span> <span style="color:#000000;">     Second Number: </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">input </span><span style="color:#FF0000;">ng-model</span><span style="color:#0000FF;">='NumberTwo'</span><span style="color:#0000FF;">&gt;&lt;/</span><span style="color:#800000;">input</span><span style="color:#0000FF;">&gt;&lt;</span><span style="color:#800000;">button </span><span style="color:#FF0000;">ng-click</span><span style="color:#0000FF;">='Multiply()'</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">Multiply</span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">button</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">5</span> <span style="color:#000000;">     Decimals : </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">input </span><span style="color:#FF0000;">ng-model</span><span style="color:#0000FF;">='Decimals'</span><span style="color:#0000FF;">&gt;&lt;/</span><span style="color:#800000;">input</span><span style="color:#0000FF;">&gt;&lt;</span><span style="color:#800000;">br</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">6</span> <span style="color:#000000;">     Result: {{ Result | twoDigits: Decimals }}
</span><span style="color:#008080;">7</span> <span style="color:#000000;"> </span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">div</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">8</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">div</span><span style="color:#0000FF;">&gt;</span></div></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>The interesting part in the view is Line (6), this time, instead of feeding result to <em>twoDigit</em> filter, we are also passing the<em> Decimals</em> value as parameter to the filter. </p>
<p><strong>Controller:</strong></p>
<div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:615bc693-15b9-48ab-94aa-185962105c8a" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<pre style="width:859px;height:278px;background-color:White;overflow:auto;"><div><span style="color:#008080;"> 1</span> <span style="color:#000000;">angular.module(</span><span style="color:#000000;">'</span><span style="color:#000000;">customFilters</span><span style="color:#000000;">'</span><span style="color:#000000;">, []).
</span><span style="color:#008080;"> 2</span> <span style="color:#000000;">    filter(</span><span style="color:#000000;">'</span><span style="color:#000000;">twoDigits</span><span style="color:#000000;">'</span><span style="color:#000000;">, </span><span style="color:#0000FF;">function</span><span style="color:#000000;">() {
</span><span style="color:#008080;"> 3</span> <span style="color:#000000;">        </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> </span><span style="color:#0000FF;">function</span><span style="color:#000000;">(input, decimals) {
</span><span style="color:#008080;"> 4</span> <span style="color:#000000;">            </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> input.toFixed(decimals);
</span><span style="color:#008080;"> 5</span> <span style="color:#000000;">        };
</span><span style="color:#008080;"> 6</span> <span style="color:#000000;">    });
</span><span style="color:#008080;"> 7</span> <span style="color:#000000;"></span><span style="color:#0000FF;">function</span><span style="color:#000000;"> filterCntl($scope) {
</span><span style="color:#008080;"> 8</span> <span style="color:#000000;">    $scope.NumberOne </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">0</span><span style="color:#000000;">;
</span><span style="color:#008080;"> 9</span> <span style="color:#000000;">    $scope.NumberTwo </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">0</span><span style="color:#000000;">;
</span><span style="color:#008080;">10</span> <span style="color:#000000;">    $scope.Result </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">0</span><span style="color:#000000;">;
</span><span style="color:#008080;">11</span> <span style="color:#000000;">    $scope.Decimals </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">2</span><span style="color:#000000;">;
</span><span style="color:#008080;">12</span> <span style="color:#000000;">    $scope.Multiply </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#0000FF;">function</span><span style="color:#000000;"> () {
</span><span style="color:#008080;">13</span> <span style="color:#000000;">        $scope.Result </span><span style="color:#000000;">=</span><span style="color:#000000;"> $scope.NumberOne </span><span style="color:#000000;">*</span><span style="color:#000000;"> $scope.NumberTwo;
</span><span style="color:#008080;">14</span> <span style="color:#000000;">    };
</span><span style="color:#008080;">15</span> <span style="color:#000000;">}</span></div></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>Line (3) of the controller now takes two parameters, <em>first argument will always be the input</em> and second one is number of digits. For sake of simplicity I am not checking isNaN etc., in the ‘<em>twoDigits</em>’ method. Here is the <a href="http://jsfiddle.net/unni/tTQQK/">jsFiddle</a> of the solution.</p>
<p>Suppose you may want to pass more than one argument to the filter function, then you will simple concatenate the arguments as </p>
<p><strong><em>{{ input | filter: arg1 : arg2 : arg2}}</em></strong>&#160;</p>
<p>I found filter to be very powerful and useful.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/csharprambling.wordpress.com/495/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/csharprambling.wordpress.com/495/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=csharprambling.wordpress.com&#038;blog=18899480&#038;post=495&#038;subd=csharprambling&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://csharprambling.wordpress.com/2013/04/09/silverlight-to-angular-5-ivalueconverter-filter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/11e06646d58e4efc69ef59a8da40a25c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ksunair</media:title>
		</media:content>
	</item>
		<item>
		<title>Silverlight to Angular &#8211; 4 (DataTemplates)</title>
		<link>http://csharprambling.wordpress.com/2013/04/07/silverlight-to-angular-4-datatemplates/</link>
		<comments>http://csharprambling.wordpress.com/2013/04/07/silverlight-to-angular-4-datatemplates/#comments</comments>
		<pubDate>Sun, 07 Apr 2013 23:05:00 +0000</pubDate>
		<dc:creator>Unni</dc:creator>
				<category><![CDATA[AngularJS]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[ng-repeat]]></category>
		<category><![CDATA[Silvelright]]></category>

		<guid isPermaLink="false">https://csharprambling.wordpress.com/?p=494</guid>
		<description><![CDATA[In Silverlight, when you are rendering some complicated data in a control, you will use DataTemplate to make it more readable to the user. One simple example would be, if you have a ListBox and you normally put a single &#8230; <a href="http://csharprambling.wordpress.com/2013/04/07/silverlight-to-angular-4-datatemplates/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=csharprambling.wordpress.com&#038;blog=18899480&#038;post=494&#038;subd=csharprambling&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In Silverlight, when you are rendering some complicated data in a control, you will use DataTemplate to make it more readable to the user. One simple example would be, if you have a ListBox and you normally put a single property like state or year etc.,. But you will run into a situation, where you may want to display more than one property per row. One such example is <a href="http://weblogs.asp.net/scottgu/pages/silverlight-tutorial-part-5-using-the-listbox-and-databinding-to-display-list-data.aspx">displaying Diggs story</a> to demonstrate the use of Listbox in Silverlight. For sake of simplicity, we will look a very simple example.</p>
<p><strong>Problem: Display all students with their ID and Name in a listbox to select.</strong></p>
<p><strong>Silverlight:</strong></p>
<p>By default, when you use ListBox in silverlight, you can bind it to a collection and point to a property of the object in the collection to display. If you want to display more than one properties from the object, then you need to use ItemTemplate. That is what we are going to do. For this example, we have a collection of Students, with following properties</p>
<p>
<div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:50fba4f9-b41e-4898-8a8b-9a3174f7ff63" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<pre style="width:859px;height:135px;background-color:White;overflow:auto;"><div><span style="color:#008080;">1</span> <span style="color:#000000;">    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">class</span><span style="color:#000000;"> Student
</span><span style="color:#008080;">2</span> <span style="color:#000000;">    {
</span><span style="color:#008080;">3</span> <span style="color:#000000;">        </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">string</span><span style="color:#000000;"> Id { </span><span style="color:#0000FF;">get</span><span style="color:#000000;">; </span><span style="color:#0000FF;">set</span><span style="color:#000000;">; }
</span><span style="color:#008080;">4</span> <span style="color:#000000;">        </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">string</span><span style="color:#000000;"> Name { </span><span style="color:#0000FF;">get</span><span style="color:#000000;">; </span><span style="color:#0000FF;">set</span><span style="color:#000000;">; }
</span><span style="color:#008080;">5</span> <span style="color:#000000;">        </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">string</span><span style="color:#000000;"> Grade { </span><span style="color:#0000FF;">get</span><span style="color:#000000;">; </span><span style="color:#0000FF;">set</span><span style="color:#000000;">; }
</span><span style="color:#008080;">6</span> <span style="color:#000000;">    }</span></div></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>Since we need to work with collection to bind to control’s itemsource, we will create an observable collection of students like the following</p>
<div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:b0d7cc79-1f34-4b59-bb74-58e0264e6e51" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<pre style="width:859px;height:193px;background-color:White;overflow:auto;"><div><span style="color:#008080;"> 1</span> <span style="color:#000000;">    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">class</span><span style="color:#000000;"> Students:ObservableCollection</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">Student</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 2</span> <span style="color:#000000;">    {
</span><span style="color:#008080;"> 3</span> <span style="color:#000000;">        </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> Students()
</span><span style="color:#008080;"> 4</span> <span style="color:#000000;">        {
</span><span style="color:#008080;"> 5</span> <span style="color:#000000;">            Add(</span><span style="color:#0000FF;">new</span><span style="color:#000000;"> Student() {Id </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#800000;">&quot;</span><span style="color:#800000;">1</span><span style="color:#800000;">&quot;</span><span style="color:#000000;">, Name </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#800000;">&quot;</span><span style="color:#800000;">First</span><span style="color:#800000;">&quot;</span><span style="color:#000000;">, Grade </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#800000;">&quot;</span><span style="color:#800000;">A</span><span style="color:#800000;">&quot;</span><span style="color:#000000;">});
</span><span style="color:#008080;"> 6</span> <span style="color:#000000;">            Add(</span><span style="color:#0000FF;">new</span><span style="color:#000000;"> Student() {Id </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#800000;">&quot;</span><span style="color:#800000;">2</span><span style="color:#800000;">&quot;</span><span style="color:#000000;">, Name </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#800000;">&quot;</span><span style="color:#800000;">Second</span><span style="color:#800000;">&quot;</span><span style="color:#000000;">, Grade </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#800000;">&quot;</span><span style="color:#800000;">B</span><span style="color:#800000;">&quot;</span><span style="color:#000000;"> });
</span><span style="color:#008080;"> 7</span> <span style="color:#000000;">            Add(</span><span style="color:#0000FF;">new</span><span style="color:#000000;"> Student() { Id </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#800000;">&quot;</span><span style="color:#800000;">3</span><span style="color:#800000;">&quot;</span><span style="color:#000000;">, Name </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#800000;">&quot;</span><span style="color:#800000;">Third</span><span style="color:#800000;">&quot;</span><span style="color:#000000;">, Grade </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#800000;">&quot;</span><span style="color:#800000;">C</span><span style="color:#800000;">&quot;</span><span style="color:#000000;"> });
</span><span style="color:#008080;"> 8</span> <span style="color:#000000;">            
</span><span style="color:#008080;"> 9</span> <span style="color:#000000;">        }
</span><span style="color:#008080;">10</span> <span style="color:#000000;">    }</span></div></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>View: </p>
<p>In our View, we will create a Listbox control and bind the Students collection to it. We are planning to display for now, just the Id and Name. Since the control’s DisplayMembersPath limitation, we will create a ItemTemplate to host the data template.</p>
<div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:d0d3985c-ae68-4558-b24e-873ab68af337" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<pre style="width:859px;height:262px;background-color:White;overflow:auto;"><div><span style="color:#008080;"> 1</span> <span style="color:#000000;">        </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">ListBox </span><span style="color:#FF0000;">ItemsSource</span><span style="color:#0000FF;">=&quot;{Binding CurrentStudents}&quot;</span><span style="color:#FF0000;"> Grid.Column</span><span style="color:#0000FF;">=&quot;1&quot;</span><span style="color:#FF0000;"> Margin</span><span style="color:#0000FF;">=&quot;36,5,36,10&quot;</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 2</span> <span style="color:#000000;">            </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">ListBox.ItemTemplate</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 3</span> <span style="color:#000000;">                </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">DataTemplate</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 4</span> <span style="color:#000000;">                    </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">Grid</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 5</span> <span style="color:#000000;">                        </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">Grid.ColumnDefinitions</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 6</span> <span style="color:#000000;">                            </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">ColumnDefinition </span><span style="color:#FF0000;">Width</span><span style="color:#0000FF;">=&quot;20*&quot;</span><span style="color:#0000FF;">&gt;&lt;/</span><span style="color:#800000;">ColumnDefinition</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 7</span> <span style="color:#000000;">                            </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">ColumnDefinition </span><span style="color:#FF0000;">Width</span><span style="color:#0000FF;">=&quot;*&quot;</span><span style="color:#0000FF;">&gt;&lt;/</span><span style="color:#800000;">ColumnDefinition</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 8</span> <span style="color:#000000;">                        </span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">Grid.ColumnDefinitions</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 9</span> <span style="color:#000000;">                        </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">TextBlock </span><span style="color:#FF0000;">Text</span><span style="color:#0000FF;">=&quot;{Binding Id}&quot;</span><span style="color:#FF0000;"> Grid.Column</span><span style="color:#0000FF;">=&quot;0&quot;</span><span style="color:#0000FF;">&gt;&lt;/</span><span style="color:#800000;">TextBlock</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">10</span> <span style="color:#000000;">                        </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">TextBlock </span><span style="color:#FF0000;">Text</span><span style="color:#0000FF;">=&quot;{Binding Name}&quot;</span><span style="color:#FF0000;"> Grid.Column</span><span style="color:#0000FF;">=&quot;1&quot;</span><span style="color:#0000FF;">&gt;&lt;/</span><span style="color:#800000;">TextBlock</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">11</span> <span style="color:#000000;">                    </span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">Grid</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">12</span> <span style="color:#000000;">                </span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">DataTemplate</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">13</span> <span style="color:#000000;">            </span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">ListBox.ItemTemplate</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">14</span> <span style="color:#000000;">        </span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">ListBox</span><span style="color:#0000FF;">&gt;</span></div></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>Nothing special here, We assigned the ObservableCollection of Students to the ListBox, and then we create a gird inside each row of ListBox and display Id and Name.</p>
<p><strong>ViewModel:</strong></p>
<p>This exposes a property for CurrentStudents, nothing else</p>
<div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:3e14a553-159e-48bf-9757-f41b7999be88" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<pre style="width:859px;height:146px;background-color:White;overflow:auto;"><div><span style="color:#008080;">1</span> <span style="color:#000000;">        </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> ObservableCollection</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">Student</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> CurrentStudents
</span><span style="color:#008080;">2</span> <span style="color:#000000;">        {
</span><span style="color:#008080;">3</span> <span style="color:#000000;">            </span><span style="color:#0000FF;">get</span><span style="color:#000000;">
</span><span style="color:#008080;">4</span> <span style="color:#000000;">            {
</span><span style="color:#008080;">5</span> <span style="color:#000000;">                </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> </span><span style="color:#0000FF;">new</span><span style="color:#000000;"> Students();
</span><span style="color:#008080;">6</span> <span style="color:#000000;">            }
</span><span style="color:#008080;">7</span> <span style="color:#000000;">        }</span></div></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>Most of the work we need to do is in the View to tell the Silverlight to create each row of ListBox with more than one property of the object collection.</p>
<p><strong>AngularJS:</strong></p>
<p>To my surprise, the amount of code we need to write to achieve the same is very less in Angular, there is not a lot of ceremony in here. The only and the main difference here between Silverlight and Angular is the building elements for the collection. In Silverlight we just pass the collection information and property to use in XAML and Silverlight takes care of traversing through the collection and building each row and putting the values property. Angular behaves same way except except instead of just pointing to the collection as itemssource, in angular, you will write the ‘foreach item in collection’ in the element instead of just the collection. Other than that everything from programming side is same. With that lets look at the view. The main difference is in the view, so lets make sure we understand it.</p>
<div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:a170f5cf-7e85-42e1-b632-a7aa56fbca31" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<pre style="width:859px;height:328px;background-color:White;overflow:auto;"><div><span style="color:#008080;"> 1</span> <span style="color:#0000FF;">&lt;!</span><span style="color:#FF00FF;">DOCTYPE html</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 2</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">html </span><span style="color:#FF0000;">ng-app</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 3</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">head</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 4</span> <span style="color:#000000;">    </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">title</span><span style="color:#0000FF;">&gt;&lt;/</span><span style="color:#800000;">title</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 5</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">head</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 6</span> <span style="color:#000000;">    </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">body</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 7</span> <span style="color:#000000;">        </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">div </span><span style="color:#FF0000;">ng-controller</span><span style="color:#0000FF;">=&quot;TemplateCtrl&quot;</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 8</span> <span style="color:#000000;">            </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">select </span><span style="color:#FF0000;">ng-model</span><span style="color:#0000FF;">=&quot;SelectedItem&quot;</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 9</span> <span style="color:#000000;">                </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">option </span><span style="color:#FF0000;">ng-repeat</span><span style="color:#0000FF;">=&quot;student in Students&quot;</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">10</span> <span style="color:#000000;">                    {{student.Id}}-{{student.Name}}
</span><span style="color:#008080;">11</span> <span style="color:#000000;">                </span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">option</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">12</span> <span style="color:#000000;">            </span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">select</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">13</span> <span style="color:#000000;">        </span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">div</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">14</span> <span style="color:#000000;">        </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">script </span><span style="color:#FF0000;">src</span><span style="color:#0000FF;">=&quot;https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js&quot;</span><span style="color:#0000FF;">&gt;&lt;/</span><span style="color:#800000;">script</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">15</span> <span style="color:#000000;">        </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">script </span><span style="color:#FF0000;">src</span><span style="color:#0000FF;">=&quot;Scripts/TemplateCtrl.js&quot;</span><span style="color:#0000FF;">&gt;&lt;/</span><span style="color:#800000;">script</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">16</span> <span style="color:#000000;">    </span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">body</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">17</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">html</span><span style="color:#0000FF;">&gt;</span></div></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>If you are following my previous blogs, you should be ok till line (8). Line (8) specifies that you are creating a list box (HTML – Select) and ignore the rest in line(8) for now. Line (9) – (11) specifies, what are the things goes into each and every options. </p>
<blockquote>
<p>&lt;option <strong>ng-repeat</strong>=&quot;student in Students&quot;&gt;</p>
</blockquote>
<p>In line (9) there is a new directive that we haven’t used before. <a href="http://docs.angularjs.org/api/ng.directive:ngRepeat">ng-repeat</a>, tells angular to create individual element for each and every object in a collection. In this example, angular will create individual option element for every object in<strong><em> ‘Students’</em></strong> collection based on the template specified in line (10). So if we are going by the collection we created in the Silverlight view model, there are 3 students which means, Angular will create 3 option elements. Line (10) is a simple template, which does nothing but a simple one way binding of Id and Name properties of student class.</p>
<p>ViewModel:</p>
<p>As you can expect, most of the work is in the View since template work is UI related and ViewModel does not have any idea about templates. So our view model looks like the following</p>
<div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:5bf7d45b-e138-4803-937e-d07c16f0de14" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<pre style="width:859px;height:122px;background-color:White;overflow:auto;"><div><span style="color:#008080;">1</span> <span style="color:#0000FF;">function</span><span style="color:#000000;"> TemplateCtrl($scope) {
</span><span style="color:#008080;">2</span> <span style="color:#000000;">    $scope.Students </span><span style="color:#000000;">=</span><span style="color:#000000;"> [
</span><span style="color:#008080;">3</span> <span style="color:#000000;">    { Id: </span><span style="color:#000000;">&quot;</span><span style="color:#000000;">1</span><span style="color:#000000;">&quot;</span><span style="color:#000000;">, Name: </span><span style="color:#000000;">&quot;</span><span style="color:#000000;">First</span><span style="color:#000000;">&quot;</span><span style="color:#000000;">, Grade: </span><span style="color:#000000;">&quot;</span><span style="color:#000000;">A</span><span style="color:#000000;">&quot;</span><span style="color:#000000;"> },
</span><span style="color:#008080;">4</span> <span style="color:#000000;">    { Id: </span><span style="color:#000000;">&quot;</span><span style="color:#000000;">2</span><span style="color:#000000;">&quot;</span><span style="color:#000000;">, Name: </span><span style="color:#000000;">&quot;</span><span style="color:#000000;">Second</span><span style="color:#000000;">&quot;</span><span style="color:#000000;">, Grade: </span><span style="color:#000000;">&quot;</span><span style="color:#000000;">B</span><span style="color:#000000;">&quot;</span><span style="color:#000000;"> },
</span><span style="color:#008080;">5</span> <span style="color:#000000;">    {Id: </span><span style="color:#000000;">&quot;</span><span style="color:#000000;">3</span><span style="color:#000000;">&quot;</span><span style="color:#000000;">, Name: </span><span style="color:#000000;">&quot;</span><span style="color:#000000;">Third</span><span style="color:#000000;">&quot;</span><span style="color:#000000;">, Grade: </span><span style="color:#000000;">&quot;</span><span style="color:#000000;">C</span><span style="color:#000000;">&quot;</span><span style="color:#000000;">}];
</span><span style="color:#008080;">6</span> <span style="color:#000000;">}</span></div></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>I skipped ‘SlectedItem’ on purpose for now.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/csharprambling.wordpress.com/494/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/csharprambling.wordpress.com/494/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=csharprambling.wordpress.com&#038;blog=18899480&#038;post=494&#038;subd=csharprambling&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://csharprambling.wordpress.com/2013/04/07/silverlight-to-angular-4-datatemplates/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/11e06646d58e4efc69ef59a8da40a25c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ksunair</media:title>
		</media:content>
	</item>
		<item>
		<title>Silverlight to Angular &#8211; 3 (Commands)</title>
		<link>http://csharprambling.wordpress.com/2013/04/03/silverlight-to-angular-3-commands/</link>
		<comments>http://csharprambling.wordpress.com/2013/04/03/silverlight-to-angular-3-commands/#comments</comments>
		<pubDate>Wed, 03 Apr 2013 17:54:00 +0000</pubDate>
		<dc:creator>Unni</dc:creator>
				<category><![CDATA[AngularJS]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Command]]></category>
		<category><![CDATA[Silvelright]]></category>

		<guid isPermaLink="false">https://csharprambling.wordpress.com/?p=493</guid>
		<description><![CDATA[In Silverlight, we use Commands to do clean separation of View and ViewModel. If we were not using commanding, we will end up creating code behind code to handle the events fired by the controls and then publish the message &#8230; <a href="http://csharprambling.wordpress.com/2013/04/03/silverlight-to-angular-3-commands/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=csharprambling.wordpress.com&#038;blog=18899480&#038;post=493&#038;subd=csharprambling&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In Silverlight, we use Commands to do clean separation of View and ViewModel. If we were not using commanding, we will end up creating code behind code to handle the events fired by the controls and then publish the message from view to viewmodel or call viewmodel method from view. Either way, there is unnecessary code in code behind. With command, we can bind the command to a control event using magic black box (in my case Jounce or your ICommand implementation) which will take care of the wiring control event directly to viewmodel method, leaving our code very clean.</p>
<p>We will reuse the code we did in the last blog since it already has commanding implementation. If you want to know more about how to do commanding, please take a look at the <a href="http://jounce.codeplex.com/wikipage?title=Commands&amp;referringTitle=Documentation">Jounce Command documentation</a> or <a href="http://csharprambling.wordpress.com/2010/11/11/obey-the-commands-in-jounce-3/">one of my old blog on commanding</a>.</p>
<p>Problem: Display number of times a button is clicked.</p>
<p>I am not going to explain the Silverlight solution here since it is already explained <a href="http://csharprambling.wordpress.com/2013/04/02/silverlight-to-angular-2-data-binding/">here</a>. Let’s look at the AngularJS solution here since I conveniently skipped it in the previous blog.</p>
<p>View:</p>
<div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:4d124c63-3567-467c-9974-755217d2a761" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<pre style="width:859px;height:251px;background-color:White;overflow:auto;"><div><span style="color:#008080;"> 1</span> <span style="color:#0000FF;">&lt;!</span><span style="color:#FF00FF;">DOCTYPE html</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 2</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">html </span><span style="color:#FF0000;">ng-app</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 3</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">head</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 4</span> <span style="color:#000000;">    </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">title</span><span style="color:#0000FF;">&gt;&lt;/</span><span style="color:#800000;">title</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 5</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">head</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 6</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">body</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 7</span> <span style="color:#000000;">    </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">div </span><span style="color:#FF0000;">ng-controller</span><span style="color:#0000FF;">=&quot;Controller&quot;</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 8</span> <span style="color:#000000;">        Number of Clicks:{{NumberOfClicks}}
</span><span style="color:#008080;"> 9</span> <span style="color:#000000;">        </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">button </span><span style="color:#FF0000;">ng-click</span><span style="color:#0000FF;">=&quot;onClick()&quot;</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">Click Me</span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">button</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">10</span> <span style="color:#000000;">    </span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">div</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">11</span> <span style="color:#000000;">    </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">script </span><span style="color:#FF0000;">src</span><span style="color:#0000FF;">=&quot;https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js&quot;</span><span style="color:#0000FF;">&gt;&lt;/</span><span style="color:#800000;">script</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">12</span> <span style="color:#000000;">    </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">script </span><span style="color:#FF0000;">src</span><span style="color:#0000FF;">=&quot;Scripts/Controller.js&quot;</span><span style="color:#0000FF;">&gt;&lt;/</span><span style="color:#800000;">script</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">13</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">body</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">14</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">html</span><span style="color:#0000FF;">&gt;</span></div></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>In this example we are interested in line (9). Angular provides a cool directive <strong>‘ng-click’</strong> which will listen to the click event and calls a function in the controller or if it is an expression then it evaluate the expression. It is as simple as that. So in Angular, if you want to use commanding to do clean separation of view and controller, all you have to do is use ng-click directive on a button. From view model perspective, the function view is calling nothing more than just another function and nothing special about it. There are bunch of APIs you can use to create this commanding behavior on different controls and you can find them all here in <a href="http://docs.angularjs.org/api/">Angular API directive</a>.</p>
<p>Controller:</p>
<div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:21a55590-8512-4d55-969d-7c054d517dea" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<pre style="width:859px;height:135px;background-color:White;overflow:auto;"><div><span style="color:#008080;">1</span> <span style="color:#0000FF;">function</span><span style="color:#000000;"> Controller($scope) {
</span><span style="color:#008080;">2</span> <span style="color:#000000;">    $scope.NumberOfClicks </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">5</span><span style="color:#000000;">;
</span><span style="color:#008080;">3</span> <span style="color:#000000;">    $scope.onClick </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#0000FF;">function</span><span style="color:#000000;">() {
</span><span style="color:#008080;">4</span> <span style="color:#000000;">        $scope.NumberOfClicks </span><span style="color:#000000;">=</span><span style="color:#000000;"> $scope.NumberOfClicks </span><span style="color:#000000;">+</span><span style="color:#000000;"> </span><span style="color:#000000;">1</span><span style="color:#000000;">;
</span><span style="color:#008080;">5</span> <span style="color:#000000;">    };
</span><span style="color:#008080;">6</span> <span style="color:#000000;">}</span></div></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>In Silverlight, not only we can execute the action through command, we have another option enable the command or not. This can be achieved using <strong>ng-disabled</strong> directive. Will look at how to use that in the following example. One benefit in Angular is that you can pass as many parameters as you want in the function call instead of just one in Silverlight. Let’s take a look at a sample where we can pass multiple parameters in function call.</p>
<p>Following example allow user to enter two numbers and perform an addition and display result. When the user enters non numeric number we want to disable the ‘Sum’ button.</p>
</p>
<p>View:</p>
<div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:85cf9584-04ad-40b0-a7a0-89a5901d84c0" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<pre style="width:859px;height:272px;background-color:White;overflow:auto;"><div><span style="color:#008080;"> 1</span> <span style="color:#0000FF;">&lt;!</span><span style="color:#FF00FF;">DOCTYPE html</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 2</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">html </span><span style="color:#FF0000;">ng-app</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 3</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">head</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 4</span> <span style="color:#000000;">    </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">title</span><span style="color:#0000FF;">&gt;&lt;/</span><span style="color:#800000;">title</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 5</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">head</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 6</span> <span style="color:#000000;">    </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">body </span><span style="color:#FF0000;">ng-controller</span><span style="color:#0000FF;">=&quot;Controller&quot;</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 7</span> <span style="color:#000000;">        First : </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">input </span><span style="color:#FF0000;">ng-model</span><span style="color:#0000FF;">=&quot;numberOne&quot;</span><span style="color:#0000FF;">/&gt;&lt;</span><span style="color:#800000;">br</span><span style="color:#0000FF;">/&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 8</span> <span style="color:#000000;">        Second: </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">input </span><span style="color:#FF0000;">ng-model</span><span style="color:#0000FF;">=&quot;numberTwo&quot;</span><span style="color:#0000FF;">/&gt;&lt;</span><span style="color:#800000;">button </span><span style="color:#FF0000;">ng-disabled</span><span style="color:#0000FF;">=&quot;IsValid(numberOne, numberTwo)&quot;</span><span style="color:#FF0000;"> 
</span><span style="color:#008080;"> 9</span> <span style="color:#FF0000;">            ng-click</span><span style="color:#0000FF;">=&quot;onClick(numberOne, numberTwo)&quot;</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">Sum</span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">button</span><span style="color:#0000FF;">&gt;&lt;</span><span style="color:#800000;">br</span><span style="color:#0000FF;">/&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">10</span> <span style="color:#000000;">        Sum: {{sum}}
</span><span style="color:#008080;">11</span> <span style="color:#000000;">        </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">script </span><span style="color:#FF0000;">src</span><span style="color:#0000FF;">=&quot;https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js&quot;</span><span style="color:#0000FF;">&gt;&lt;/</span><span style="color:#800000;">script</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">12</span> <span style="color:#000000;">        </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">script </span><span style="color:#FF0000;">src</span><span style="color:#0000FF;">=&quot;Scripts/Controller.js&quot;</span><span style="color:#0000FF;">&gt;&lt;/</span><span style="color:#800000;">script</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">13</span> <span style="color:#000000;">    </span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">body</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">14</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">html</span><span style="color:#0000FF;">&gt;</span></div></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>Both first and second number are two-way binding since the user change need to be propagated to model. On button click, we are using <strong>ng-click</strong> directive to call <em>‘onClick’</em> function in the controller and we are passing two parameters numberOne and numberTwo from the UI. If you notice, we also have another directive called <strong>ng-disabled</strong>, this will make the element disable if it return associated function from control returns false. If the input data collection is a form then you can enable and disable form submit button using Form.$validate.</p>
</p>
</p>
<p>Controller:</p>
<p>This is nothing but simple java script code which has variable assignments and a function call.</p>
</p>
<div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:679173bc-a8d9-4a6c-93da-ecaa91bb1f15" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<pre style="width:859px;height:272px;background-color:White;overflow:auto;"><div><span style="color:#008080;"> 1</span> <span style="color:#0000FF;">function</span><span style="color:#000000;"> Controller($scope) {
</span><span style="color:#008080;"> 2</span> <span style="color:#000000;">    $scope.numberOne </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">0</span><span style="color:#000000;">;
</span><span style="color:#008080;"> 3</span> <span style="color:#000000;">    $scope.numberTwo </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">0</span><span style="color:#000000;">;
</span><span style="color:#008080;"> 4</span> <span style="color:#000000;">    $scope.sum </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">0</span><span style="color:#000000;">;
</span><span style="color:#008080;"> 5</span> <span style="color:#000000;">    $scope.onClick </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#0000FF;">function</span><span style="color:#000000;">(first, second) {
</span><span style="color:#008080;"> 6</span> <span style="color:#000000;">        $scope.sum </span><span style="color:#000000;">=</span><span style="color:#000000;"> Number(first) </span><span style="color:#000000;">+</span><span style="color:#000000;">Number(second);
</span><span style="color:#008080;"> 7</span> <span style="color:#000000;">    };
</span><span style="color:#008080;"> 8</span> <span style="color:#000000;">    $scope.IsValid </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#0000FF;">function</span><span style="color:#000000;">(first, second) {
</span><span style="color:#008080;"> 9</span> <span style="color:#000000;">        </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> </span><span style="color:#000000;">!</span><span style="color:#000000;">(isNumber(first) </span><span style="color:#000000;">&amp;&amp;</span><span style="color:#000000;"> isNumber(second));
</span><span style="color:#008080;">10</span> <span style="color:#000000;">    };
</span><span style="color:#008080;">11</span> <span style="color:#000000;">    </span><span style="color:#0000FF;">function</span><span style="color:#000000;"> isNumber(n) {
</span><span style="color:#008080;">12</span> <span style="color:#000000;">        </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> </span><span style="color:#000000;">!</span><span style="color:#000000;">isNaN(parseFloat(n)) </span><span style="color:#000000;">&amp;&amp;</span><span style="color:#000000;"> isFinite(n);
</span><span style="color:#008080;">13</span> <span style="color:#000000;">    }
</span><span style="color:#008080;">14</span> <span style="color:#000000;">}</span></div></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>One another point of interest along the line of ng-click is about other events from other elements. Take a look at this <a href="http://stackoverflow.com/questions/14346073/angularjs-is-ng-click-a-good-practice-why-is-there-no-ng-event-in-angularj">stack overflow question and answer</a>. At the end, there is a comment by Tim Steward which is very interesting. He pointed out that if you do not see any ng- function for the element you are working on, his suggestion is to use <a href="http://angular-ui.github.com/#/directives-event">Angular-UI</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/csharprambling.wordpress.com/493/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/csharprambling.wordpress.com/493/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=csharprambling.wordpress.com&#038;blog=18899480&#038;post=493&#038;subd=csharprambling&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://csharprambling.wordpress.com/2013/04/03/silverlight-to-angular-3-commands/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/11e06646d58e4efc69ef59a8da40a25c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ksunair</media:title>
		</media:content>
	</item>
		<item>
		<title>Silverlight to Angular &#8211; 2 (Data Binding)</title>
		<link>http://csharprambling.wordpress.com/2013/04/02/silverlight-to-angular-2-data-binding/</link>
		<comments>http://csharprambling.wordpress.com/2013/04/02/silverlight-to-angular-2-data-binding/#comments</comments>
		<pubDate>Tue, 02 Apr 2013 19:33:00 +0000</pubDate>
		<dc:creator>Unni</dc:creator>
				<category><![CDATA[AngularJS]]></category>
		<category><![CDATA[Data Binding]]></category>
		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">https://csharprambling.wordpress.com/?p=492</guid>
		<description><![CDATA[One of the main features in Silverlight is binding. There are two ways you do data binding. ‘One way’ or ‘two way’ data binding. There is one more ‘onetime’, I am skipping it on purpose here. Let’s first see what &#8230; <a href="http://csharprambling.wordpress.com/2013/04/02/silverlight-to-angular-2-data-binding/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=csharprambling.wordpress.com&#038;blog=18899480&#038;post=492&#038;subd=csharprambling&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>One of the main features in Silverlight is binding. There are two ways you do data binding. <strong>‘One way’</strong> or <strong>‘two way’</strong> data binding. There is one more <em>‘onetime’</em>, I am skipping it on purpose here. Let’s first see what they are and when do you use them and then see its associated implementation in both Silverlight and Angular.JS</p>
<p><strong>One-way:</strong> As the name suggests, as the data in data model changes, the changes are reflected in the View. When it is a one way data binding, user is not allowed to change the value in the view. It is meant to be showing the data coming from the model only. The read only controls are the excellent candidates for this kind of binding. This eliminates the overhead of two way binding.</p>
<p><a href="http://csharprambling.files.wordpress.com/2013/04/image.png"><img title="image" style="border-top:0;border-right:0;border-bottom:0;border-left:0;display:inline;" border="0" alt="image" src="http://csharprambling.files.wordpress.com/2013/04/image_thumb.png?w=332&#038;h=119" width="332" height="119" /></a> </p>
<p><strong>Problem: Display number of times a button is clicked using one-way binding.</strong> </p>
<p><strong>Silverlight:</strong></p>
<p><strong>View:</strong> For our problem, we need three controls.</p>
<ol>
<li>A text block with text specifying the ‘Number of clicks:’ </li>
<li>A text block which displays number of clicks itself. </li>
<li>A button to click to modify the data. </li>
</ol>
<blockquote><div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:dedcfd18-e0ff-462a-8bff-81e8444a660f" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<pre style="width:1064px;height:75px;background-color:White;overflow:auto;"><div><span style="color:#008080;">1</span> <span style="color:#0000FF;">&lt;</span><span style="color:#800000;">TextBlock </span><span style="color:#FF0000;">Grid.Row</span><span style="color:#0000FF;">=&quot;0&quot;</span><span style="color:#FF0000;"> Grid.Column</span><span style="color:#0000FF;">=&quot;0&quot;</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">Number of Clicks: </span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">TextBlock</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">2</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">TextBlock  </span><span style="color:#FF0000;">Grid.Row</span><span style="color:#0000FF;">=&quot;0&quot;</span><span style="color:#FF0000;"> Text</span><span style="color:#0000FF;">=&quot;</span><span style="color:#808000;">{Binding NumberOfClicks, Mode=OneWay}</span><span style="color:#0000FF;">&quot;</span><span style="color:#FF0000;"> Grid.Column</span><span style="color:#0000FF;">=&quot;1&quot;</span><span style="color:#0000FF;">/&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">3</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">Button </span><span style="color:#FF0000;">Grid.Column</span><span style="color:#0000FF;">=&quot;1&quot;</span><span style="color:#FF0000;"> Grid.Row</span><span style="color:#0000FF;">=&quot;1&quot;</span><span style="color:#FF0000;"> Command</span><span style="color:#0000FF;">=&quot;</span><span style="color:#808000;">{Binding ClickMe}</span><span style="color:#0000FF;">&quot;</span><span style="color:#FF0000;"> CommandParameter</span><span style="color:#0000FF;">=&quot;&quot;</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">Click me</span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">Button</span><span style="color:#0000FF;">&gt;</span></div></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</blockquote>
<p><strong>ViewModel:</strong> We have a public property called ‘NumberOfClicks’ which is what bound to our text block to show number of times the button clicked. The second one is IActionCommand to trigger click event for the button. Every time button clicked, we increment the counter and raise the property changed event so that view can pick up the modified value.</p>
<div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:fe0e546a-fffd-4c60-9449-e8d772942b00" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<pre style="width:1064px;height:311px;background-color:White;overflow:auto;"><div><span style="color:#008080;"> 1</span> <span style="color:#000000;">[ExportAsViewModel(</span><span style="color:#0000FF;">typeof</span><span style="color:#000000;">(MainPageViewModel))]
</span><span style="color:#008080;"> 2</span> <span style="color:#000000;"></span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">class</span><span style="color:#000000;"> MainPageViewModel:BaseViewModel
</span><span style="color:#008080;"> 3</span> <span style="color:#000000;">{
</span><span style="color:#008080;"> 4</span> <span style="color:#000000;">   </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> IActionCommand</span><span style="color:#000000;">&lt;</span><span style="color:#0000FF;">string</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> ClickMe { </span><span style="color:#0000FF;">get</span><span style="color:#000000;">; </span><span style="color:#0000FF;">private</span><span style="color:#000000;"> </span><span style="color:#0000FF;">set</span><span style="color:#000000;">; }
</span><span style="color:#008080;"> 5</span> <span style="color:#000000;">   </span><span style="color:#0000FF;">private</span><span style="color:#000000;"> </span><span style="color:#0000FF;">int</span><span style="color:#000000;"> _numberOfClicks </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#800080;">0</span><span style="color:#000000;">;
</span><span style="color:#008080;"> 6</span> <span style="color:#000000;">   </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">int</span><span style="color:#000000;"> NumberOfClicks
</span><span style="color:#008080;"> 7</span> <span style="color:#000000;">   {
</span><span style="color:#008080;"> 8</span> <span style="color:#000000;">      </span><span style="color:#0000FF;">get</span><span style="color:#000000;"> { </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> _numberOfClicks; }
</span><span style="color:#008080;"> 9</span> <span style="color:#000000;">      </span><span style="color:#0000FF;">set</span><span style="color:#000000;"> { _numberOfClicks </span><span style="color:#000000;">=</span><span style="color:#000000;"> value; RaisePropertyChanged(()</span><span style="color:#000000;">=&gt;</span><span style="color:#000000;">NumberOfClicks); }
</span><span style="color:#008080;">10</span> <span style="color:#000000;">   }
</span><span style="color:#008080;">11</span> <span style="color:#000000;">
</span><span style="color:#008080;">12</span> <span style="color:#000000;">   </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> MainPageViewModel()
</span><span style="color:#008080;">13</span> <span style="color:#000000;">   {
</span><span style="color:#008080;">14</span> <span style="color:#000000;">       ClickMe </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#0000FF;">new</span><span style="color:#000000;"> ActionCommand</span><span style="color:#000000;">&lt;</span><span style="color:#0000FF;">string</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">(p</span><span style="color:#000000;">=&gt;</span><span style="color:#000000;"> NumberOfClicks </span><span style="color:#000000;">=</span><span style="color:#000000;"> NumberOfClicks</span><span style="color:#000000;">+</span><span style="color:#800080;">1</span><span style="color:#000000;">);
</span><span style="color:#008080;">15</span> <span style="color:#000000;">   }
</span><span style="color:#008080;">16</span> <span style="color:#000000;">}</span></div></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p><strong>Angular:</strong></p>
<p>In Angular, to achieve one way binding, i.e., $scope -&gt; view, all you do is to use ng-bind directive instead of ng-model. Alternate solution is to use the default {{variable}} notation, which also behaves same way as the ng-bind. I prefer {{variable}} approach. Here is an example which demonstrate one way binding using ng-bind.</p>
<p><strong>View:</strong></p>
<div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:d66dbe75-113e-4d2f-b3d8-4851eee2baf8" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<pre style="width:1064px;height:311px;background-color:White;overflow:auto;"><div><span style="color:#008080;"> 1</span> <span style="color:#0000FF;">&lt;!</span><span style="color:#FF00FF;">DOCTYPE html</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 2</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">html </span><span style="color:#FF0000;">ng-app</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 3</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">head</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 4</span> <span style="color:#000000;">    </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">title</span><span style="color:#0000FF;">&gt;&lt;/</span><span style="color:#800000;">title</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 5</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">head</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 6</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">body</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 7</span> <span style="color:#000000;">    </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">div </span><span style="color:#FF0000;">ng-controller</span><span style="color:#0000FF;">=&quot;Controller&quot;</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 8</span> <span style="color:#000000;">        Number of Clicks:{{NumberOfClicks}}
</span><span style="color:#008080;"> 9</span> <span style="color:#000000;">        </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">button </span><span style="color:#FF0000;">ng-click</span><span style="color:#0000FF;">=&quot;onClick()&quot;</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">Click Me</span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">button</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">10</span> <span style="color:#000000;">    </span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">div</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">11</span> <span style="color:#000000;">    </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">script </span><span style="color:#FF0000;">src</span><span style="color:#0000FF;">=&quot;https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js&quot;</span><span style="color:#0000FF;">&gt;&lt;/</span><span style="color:#800000;">script</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">12</span> <span style="color:#000000;">    </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">script </span><span style="color:#FF0000;">src</span><span style="color:#0000FF;">=&quot;Scripts/Controller.js&quot;</span><span style="color:#0000FF;">&gt;&lt;/</span><span style="color:#800000;">script</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">13</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">body</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">14</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">html</span><span style="color:#0000FF;">&gt;</span></div></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>Line (8) is the one way binding. Even though ng-bind was not specified, it is the default binding. So you do not explicitly specify the ng-bind and I prefer to stay with {{}} approach.</p>
<p><strong>ViewModel/Controller:</strong></p>
<div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:854333d1-4b8b-4b0f-aa1d-91da81eae0ee" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<pre style="width:1064px;height:123px;background-color:White;overflow:auto;"><div><span style="color:#008080;">1</span> <span style="color:#0000FF;">function</span><span style="color:#000000;"> Controller($scope) {
</span><span style="color:#008080;">2</span> <span style="color:#000000;">    $scope.NumberOfClicks </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">5</span><span style="color:#000000;">;
</span><span style="color:#008080;">3</span> <span style="color:#000000;">    $scope.onClick </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#0000FF;">function</span><span style="color:#000000;">() {
</span><span style="color:#008080;">4</span> <span style="color:#000000;">        $scope.NumberOfClicks </span><span style="color:#000000;">=</span><span style="color:#000000;"> $scope.NumberOfClicks </span><span style="color:#000000;">+</span><span style="color:#000000;"> </span><span style="color:#000000;">1</span><span style="color:#000000;">;
</span><span style="color:#008080;">5</span> <span style="color:#000000;">    };
</span><span style="color:#008080;">6</span> <span style="color:#000000;">}</span></div></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>[Note: When you run the code, if the number is not incrementing then, one possibility is that, you might not have ‘()’ next to the onClick function in the HTML. Been there done that.]</p>
<p><strong>Two-way:</strong> This is the standard and most used data binding. In this type of binding, when a data in data model changes, the changes reflected in View. In the same way, user is allowed to change the data in view and the change pushed to Model. In my opinion this is what made Silverlight so awesome and now it carries to Angular.</p>
<p><a href="http://csharprambling.files.wordpress.com/2013/04/image1.png"><img title="image" style="border-top:0;border-right:0;border-bottom:0;border-left:0;display:inline;" border="0" alt="image" src="http://csharprambling.files.wordpress.com/2013/04/image_thumb1.png?w=378&#038;h=115" width="378" height="115" /></a> </p>
<p><strong>Problem: Get name from user and provide a greeting to the user. Allow user to add ‘!’ to the end of the name using a button. </strong></p>
<p><strong>Silverlight:</strong> For our problem we need three controls</p>
<p><strong>View:</strong></p>
<ol>
<li>A text block which display ‘Enter Name’. </li>
<li>A text box to get user name. </li>
<li>A text block to display ‘Hello {name}’. </li>
<li>A button to add ‘!’ char to the name, which should modify both the (2) and (3) control values. </li>
</ol>
<div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:857d541d-2240-49cf-9cc6-4ee9b1f81182" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<pre style="width:1064px;height:95px;background-color:White;overflow:auto;"><div><span style="color:#008080;">1</span> <span style="color:#000000;">        </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">TextBlock</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">Name: </span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">TextBlock</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">2</span> <span style="color:#000000;">        </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">TextBox </span><span style="color:#FF0000;">Grid.Column</span><span style="color:#0000FF;">=&quot;1&quot;</span><span style="color:#FF0000;"> Text</span><span style="color:#0000FF;">=&quot;</span><span style="color:#808000;">{Binding Name, Mode=TwoWay}</span><span style="color:#0000FF;">&quot;</span><span style="color:#0000FF;">&gt;&lt;/</span><span style="color:#800000;">TextBox</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">3</span> <span style="color:#000000;">        </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">Button </span><span style="color:#FF0000;">Command</span><span style="color:#0000FF;">=&quot;</span><span style="color:#808000;">{Binding ClickMe}</span><span style="color:#0000FF;">&quot;</span><span style="color:#FF0000;"> CommandParameter</span><span style="color:#0000FF;">=&quot;&quot;</span><span style="color:#FF0000;"> Grid.Column</span><span style="color:#0000FF;">=&quot;2&quot;</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">Add !</span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">Button</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">4</span> <span style="color:#000000;">        </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">TextBlock </span><span style="color:#FF0000;">Grid.Row</span><span style="color:#0000FF;">=&quot;1&quot;</span><span style="color:#FF0000;"> Text</span><span style="color:#0000FF;">=&quot;</span><span style="color:#808000;">{Binding HelloName}</span><span style="color:#0000FF;">&quot;</span><span style="color:#0000FF;">&gt;&lt;/</span><span style="color:#800000;">TextBlock</span><span style="color:#0000FF;">&gt;</span></div></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>If you notice line (2) the text box binding ‘Name’ is set to<strong> ‘TwoWay’</strong>, which means, user is allowed to enter data which will change the viewmodel, but if the data changes in the model, it get reflected back on the view as well.&#160; When you run the code, you will notice, when user enters ‘Unni’ in the name and press the button, it not only display ‘Hello Unni!’ in the text block in line (4) but also changes the text box (line 2) to ‘Unni<strong>!</strong>’. In this example, line (4) is still one way, so when data changes in the model, the change will get reflected in the view.&#8217;</p>
<p>ViewModel:</p>
<div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:db4954d6-c1e6-4408-b719-ec07f51ef6f2" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<pre style="width:1064px;height:781px;background-color:White;overflow:auto;"><div><span style="color:#008080;"> 1</span> <span style="color:#000000;">    [ExportAsViewModel(</span><span style="color:#0000FF;">typeof</span><span style="color:#000000;">(MainPageViewModel))]
</span><span style="color:#008080;"> 2</span> <span style="color:#000000;">    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">class</span><span style="color:#000000;"> MainPageViewModel:BaseViewModel
</span><span style="color:#008080;"> 3</span> <span style="color:#000000;">    {
</span><span style="color:#008080;"> 4</span> <span style="color:#000000;">        </span><span style="color:#0000FF;">private</span><span style="color:#000000;"> </span><span style="color:#0000FF;">string</span><span style="color:#000000;"> _name;
</span><span style="color:#008080;"> 5</span> <span style="color:#000000;">        </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">string</span><span style="color:#000000;"> Name
</span><span style="color:#008080;"> 6</span> <span style="color:#000000;">        {
</span><span style="color:#008080;"> 7</span> <span style="color:#000000;">            </span><span style="color:#0000FF;">get</span><span style="color:#000000;"> { </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> _name; }
</span><span style="color:#008080;"> 8</span> <span style="color:#000000;">            </span><span style="color:#0000FF;">set</span><span style="color:#000000;">
</span><span style="color:#008080;"> 9</span> <span style="color:#000000;">            {
</span><span style="color:#008080;">10</span> <span style="color:#000000;">                _name </span><span style="color:#000000;">=</span><span style="color:#000000;"> value;
</span><span style="color:#008080;">11</span> <span style="color:#000000;">                RaisePropertyChanged(() </span><span style="color:#000000;">=&gt;</span><span style="color:#000000;"> Name);
</span><span style="color:#008080;">12</span> <span style="color:#000000;">            }
</span><span style="color:#008080;">13</span> <span style="color:#000000;">        }
</span><span style="color:#008080;">14</span> <span style="color:#000000;">        </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> IActionCommand</span><span style="color:#000000;">&lt;</span><span style="color:#0000FF;">string</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> ClickMe { </span><span style="color:#0000FF;">get</span><span style="color:#000000;">; </span><span style="color:#0000FF;">private</span><span style="color:#000000;"> </span><span style="color:#0000FF;">set</span><span style="color:#000000;">; }
</span><span style="color:#008080;">15</span> <span style="color:#000000;">
</span><span style="color:#008080;">16</span> <span style="color:#000000;">        </span><span style="color:#0000FF;">private</span><span style="color:#000000;"> </span><span style="color:#0000FF;">string</span><span style="color:#000000;"> _helloName;
</span><span style="color:#008080;">17</span> <span style="color:#000000;">
</span><span style="color:#008080;">18</span> <span style="color:#000000;">        </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">string</span><span style="color:#000000;"> HelloName
</span><span style="color:#008080;">19</span> <span style="color:#000000;">        {
</span><span style="color:#008080;">20</span> <span style="color:#000000;">            </span><span style="color:#0000FF;">get</span><span style="color:#000000;"> { </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> _helloName; }
</span><span style="color:#008080;">21</span> <span style="color:#000000;">            </span><span style="color:#0000FF;">set</span><span style="color:#000000;">
</span><span style="color:#008080;">22</span> <span style="color:#000000;">            {
</span><span style="color:#008080;">23</span> <span style="color:#000000;">                _helloName </span><span style="color:#000000;">=</span><span style="color:#000000;"> value;
</span><span style="color:#008080;">24</span> <span style="color:#000000;">                RaisePropertyChanged(() </span><span style="color:#000000;">=&gt;</span><span style="color:#000000;"> HelloName);
</span><span style="color:#008080;">25</span> <span style="color:#000000;">            }
</span><span style="color:#008080;">26</span> <span style="color:#000000;">        }
</span><span style="color:#008080;">27</span> <span style="color:#000000;">
</span><span style="color:#008080;">28</span> <span style="color:#000000;">        </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> MainPageViewModel()
</span><span style="color:#008080;">29</span> <span style="color:#000000;">        {
</span><span style="color:#008080;">30</span> <span style="color:#000000;">            ClickMe </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#0000FF;">new</span><span style="color:#000000;"> ActionCommand</span><span style="color:#000000;">&lt;</span><span style="color:#0000FF;">string</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">(Clicked);
</span><span style="color:#008080;">31</span> <span style="color:#000000;">        }
</span><span style="color:#008080;">32</span> <span style="color:#000000;">
</span><span style="color:#008080;">33</span> <span style="color:#000000;">        </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">void</span><span style="color:#000000;"> Clicked(</span><span style="color:#0000FF;">string</span><span style="color:#000000;"> obj)
</span><span style="color:#008080;">34</span> <span style="color:#000000;">        {
</span><span style="color:#008080;">35</span> <span style="color:#000000;">            </span><span style="color:#0000FF;">if</span><span style="color:#000000;"> (</span><span style="color:#000000;">!</span><span style="color:#000000;">Name.EndsWith(</span><span style="color:#800000;">&quot;</span><span style="color:#800000;">!</span><span style="color:#800000;">&quot;</span><span style="color:#000000;">))
</span><span style="color:#008080;">36</span> <span style="color:#000000;">                Name </span><span style="color:#000000;">+=</span><span style="color:#000000;"> </span><span style="color:#800000;">&quot;</span><span style="color:#800000;">!</span><span style="color:#800000;">&quot;</span><span style="color:#000000;">;
</span><span style="color:#008080;">37</span> <span style="color:#000000;">            HelloName </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#800000;">&quot;</span><span style="color:#800000;">Hello </span><span style="color:#800000;">&quot;</span><span style="color:#000000;"> </span><span style="color:#000000;">+</span><span style="color:#000000;"> Name;
</span><span style="color:#008080;">38</span> <span style="color:#000000;">        }
</span><span style="color:#008080;">39</span> <span style="color:#000000;">    }</span></div></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>[Note: If you have trouble with the code, make sure, all the properties have RaisePropertyChanged event in it. Also make sure the button click command name matches the command in the view model. The last part make sure all the exports attributes are marked properly]</p>
<p><strong>Angular:</strong></p>
</p>
</p>
</p>
<p>By default, if you do not specify <strong>ng-</strong> in the element, then one way binding is assumed. It means, whenever you use {{variable}}, it is considered one way binding. Anytime data change happens in the controller, view gets the modified data. <strong>$scope -&gt; view</strong>. On the other hand if you specify ‘ng-model’ attribute in the element, it is considered two way binding. It means, user changes will be pushed to model and any model changes will be pushed to view. <strong>$scope &lt;-&gt; model</strong>. </p>
<p><strong>View:</strong></p>
<div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:fcb34636-3530-47f1-915b-5b795bf9caf9" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<pre style="width:859px;height:335px;background-color:White;overflow:auto;"><div><span style="color:#008080;"> 1</span> <span style="color:#0000FF;">&lt;!</span><span style="color:#FF00FF;">DOCTYPE html</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 2</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">html </span><span style="color:#FF0000;">ng-app</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 3</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">head</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 4</span> <span style="color:#000000;">    </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">title</span><span style="color:#0000FF;">&gt;&lt;/</span><span style="color:#800000;">title</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 5</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">head</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 6</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">body</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 7</span> <span style="color:#000000;">    </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">div </span><span style="color:#FF0000;">ng-controller</span><span style="color:#0000FF;">=&quot;controller&quot;</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 8</span> <span style="color:#000000;">        Name: </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">input </span><span style="color:#FF0000;">ng-model</span><span style="color:#0000FF;">=&quot;Name&quot;</span><span style="color:#0000FF;">/&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;"> 9</span> <span style="color:#000000;">        </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">button </span><span style="color:#FF0000;">ng-click</span><span style="color:#0000FF;">=&quot;onClick()&quot;</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">Click Me</span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">button</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">10</span> <span style="color:#000000;">        </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">br </span><span style="color:#0000FF;">/&gt;</span><span style="color:#000000;">{{HelloName}}
</span><span style="color:#008080;">11</span> <span style="color:#000000;">    </span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">div</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">12</span> <span style="color:#000000;">    </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">script </span><span style="color:#FF0000;">src</span><span style="color:#0000FF;">=&quot;https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js&quot;</span><span style="color:#0000FF;">&gt;&lt;/</span><span style="color:#800000;">script</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">13</span> <span style="color:#000000;">    </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">script </span><span style="color:#FF0000;">src</span><span style="color:#0000FF;">=&quot;scripts/controller.js&quot;</span><span style="color:#0000FF;">&gt;&lt;/</span><span style="color:#800000;">script</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">14</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">body</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#008080;">15</span> <span style="color:#000000;"></span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">html</span><span style="color:#0000FF;">&gt;</span></div></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>Line (8) is a two way binding to a variable ‘<em>Name</em>’ in the $Scope. It means, when user enters a value in the input, the user entered value get pushed to the $scope variable Name. On the other hand, line (10), when ever there is a value change to ‘<em>HelloName’</em> variable in $scope, it get pushed to the view.</p>
<p>ViewModel/Controller: (Going forward, I am going to refer this as Controller instead of ViewModel).</p>
</p>
<div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:20b8b416-6f14-4147-9d6f-ed85e70239e8" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<pre style="width:859px;height:251px;background-color:White;overflow:auto;"><div><span style="color:#008080;"> 1</span> <span style="color:#0000FF;">function</span><span style="color:#000000;"> controller($scope) {
</span><span style="color:#008080;"> 2</span> <span style="color:#000000;">    $scope.Name </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">&quot;&quot;</span><span style="color:#000000;">;
</span><span style="color:#008080;"> 3</span> <span style="color:#000000;">    $scope.HelloName </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">&quot;&quot;</span><span style="color:#000000;">;
</span><span style="color:#008080;"> 4</span> <span style="color:#000000;">
</span><span style="color:#008080;"> 5</span> <span style="color:#000000;">    $scope.onClick </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#0000FF;">function</span><span style="color:#000000;"> () {
</span><span style="color:#008080;"> 6</span> <span style="color:#000000;">        </span><span style="color:#0000FF;">debugger</span><span style="color:#000000;">;
</span><span style="color:#008080;"> 7</span> <span style="color:#000000;">        </span><span style="color:#0000FF;">if</span><span style="color:#000000;"> ($scope.Name[$scope.Name.length </span><span style="color:#000000;">-</span><span style="color:#000000;"> </span><span style="color:#000000;">1</span><span style="color:#000000;">] </span><span style="color:#000000;">!==</span><span style="color:#000000;"> </span><span style="color:#000000;">&quot;</span><span style="color:#000000;">!</span><span style="color:#000000;">&quot;</span><span style="color:#000000;">)
</span><span style="color:#008080;"> 8</span> <span style="color:#000000;">            $scope.Name </span><span style="color:#000000;">=</span><span style="color:#000000;"> $scope.Name </span><span style="color:#000000;">+</span><span style="color:#000000;"> </span><span style="color:#000000;">&quot;</span><span style="color:#000000;">!</span><span style="color:#000000;">&quot;</span><span style="color:#000000;">;
</span><span style="color:#008080;"> 9</span> <span style="color:#000000;">        $scope.HelloName </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">'</span><span style="color:#000000;">Hello </span><span style="color:#000000;">'</span><span style="color:#000000;"> </span><span style="color:#000000;">+</span><span style="color:#000000;"> $scope.Name;
</span><span style="color:#008080;">10</span> <span style="color:#000000;">    };
</span><span style="color:#008080;">11</span> <span style="color:#000000;">}</span></div></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>Initially when the HTML is rendered in the browser, both <em>Name</em> and <em>HelloName</em> will have empty string. When the user enters a value, say ‘Unni’ in the input box and click on ‘Click Me’ button, controller executes ‘<em>onClick</em>’ function and sets the <em>Name</em> to ‘Unni!’ (note ‘!’ at the end) and make ‘<em>HelloName</em>’ as ‘Hello Unni!’. So both the variables ‘<em>Name</em>’ and ‘<em>HelloName</em>’ changed in the model so the changes pushed back to view. Now if you notice, <strong><em>your input changed to Unni! Instead of Unni</em></strong>, thus making variable ‘<em>Name</em>’ two way binding. While <em>HelloName</em> pushed to view with new value making it one way binding.</p>
<p><a href="http://stackoverflow.com/questions/12419619/whats-the-difference-between-ng-model-and-ng-bind">So the bottom line is, if you use ‘ng-model’, it is two way binding otherwise it is one way binding.</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/csharprambling.wordpress.com/492/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/csharprambling.wordpress.com/492/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=csharprambling.wordpress.com&#038;blog=18899480&#038;post=492&#038;subd=csharprambling&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://csharprambling.wordpress.com/2013/04/02/silverlight-to-angular-2-data-binding/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/11e06646d58e4efc69ef59a8da40a25c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ksunair</media:title>
		</media:content>

		<media:content url="http://csharprambling.files.wordpress.com/2013/04/image_thumb.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://csharprambling.files.wordpress.com/2013/04/image_thumb1.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
		<item>
		<title>Silverlight to Angular &#8211; 1</title>
		<link>http://csharprambling.wordpress.com/2013/03/28/silverlight-to-angular-1/</link>
		<comments>http://csharprambling.wordpress.com/2013/03/28/silverlight-to-angular-1/#comments</comments>
		<pubDate>Thu, 28 Mar 2013 19:03:00 +0000</pubDate>
		<dc:creator>Unni</dc:creator>
				<category><![CDATA[AngularJS]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Silvelright]]></category>

		<guid isPermaLink="false">https://csharprambling.wordpress.com/?p=487</guid>
		<description><![CDATA[So from my previous blog you all know that I am moving towards Angular for our front end development. I am planning to write series of blogs on how to take Silverlight knowledge to Angular. But if you want to &#8230; <a href="http://csharprambling.wordpress.com/2013/03/28/silverlight-to-angular-1/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=csharprambling.wordpress.com&#038;blog=18899480&#038;post=487&#038;subd=csharprambling&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>So from my <a href="http://csharprambling.wordpress.com/2013/02/05/silverlight-to-angular-js/">previous blog</a> you all know that I am moving towards Angular for our front end development. I am planning to write series of blogs on how to take Silverlight knowledge to Angular. But if you want to learn Angular without the Silverlight background, please head to <a href="http://docs.angularjs.org/tutorial/step_00">this great example</a>. Without further ado, lets look at our hello world program.</p>
<p><strong>Problem: We are asked to create an app, which will display ‘Hello World’.</strong></p>
<p><strong>Silverlight:</strong></p>
<p>As you all know, Silverlight follows MVVM pattern. In this case, all we need to do is to display a static text ‘Hello World’. So there is no need for Model or ViewModel here. All we need is simple XAML with a TextBlock which displays ‘Hello World’ like the following</p>
<blockquote><p>&lt;Grid x:Name=&quot;LayoutRoot&quot; Background=&quot;White&quot;&gt;      <br />&#160;&#160;&#160; &lt;TextBlock HorizontalAlignment=&quot;Left&quot;&#160; TextWrapping=&quot;Wrap&quot; Text=&quot;Hello World&quot; VerticalAlignment=&quot;Top&quot;       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Height=&quot;25&quot; Width=&quot;289&quot;/&gt;       <br />&lt;/Grid&gt;</p>
</blockquote>
<p>So in here XAML is the ‘V’ ( – View) in the ‘MVVM’. Nothing complicated here, very simple. Compile and run the app and you will get a very simple ‘Hello World’.</p>
<p><strong>Angular:</strong></p>
<p>Now switching the focus to Angular world. Before we dig deep into <a href="http://angularjs.org/">Angular</a>, we need to understand what is Front End Framework (FeF). If you are to develop application using HTML, you would use HTML for rendering page, CSS for styling and Javascript to handling client side processing. To create a dynamic, responsive web page, we will need to lot of work with Javascript and ton of DOM manipulation. So here is where FeF to help. <a href="http://net.tutsplus.com/articles/web-roundups/20-javascript-frameworks-worth-checking-out/">There are ton of FeF is available today</a>. They all abstract the common tasks that we would do and make our development easy to create responsive and cross browser support (with limitation) application. With FeF, we can focus on building our application rather than worry about browsers and ugly DOM manipulation.&#160; <a href="http://angularjs.org/">Angular</a> is one such framework backed by Google. I do not want to go into details of what it gives out of the box since it is very well documented in <a href="http://angularjs.org/">angular website</a>. </p>
<p>Before we go into Angular development, have a look at a mapping between Angular and Silverlight.</p>
<h3>
<table cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td></td>
<td width="356"></td>
<td width="2"></td>
</tr>
<tr>
<td></td>
<td valign="top"><a href="http://csharprambling.files.wordpress.com/2013/03/clip_image0016.png"><img title="clip_image001[6]" style="display:inline;border-width:0;" border="0" alt="clip_image001[6]" src="http://csharprambling.files.wordpress.com/2013/03/clip_image0016_thumb.png?w=244&#038;h=63" width="244" height="63" /></a></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td></td>
<td valign="top"><a href="http://csharprambling.files.wordpress.com/2013/03/clip_image0026.png"><img title="clip_image002[6]" style="display:inline;border-width:0;" border="0" alt="clip_image002[6]" src="http://csharprambling.files.wordpress.com/2013/03/clip_image0026_thumb.png?w=244&#038;h=31" width="244" height="31" /></a></td>
</tr>
</tbody>
</table>
</h3>
<p>Angular framework also support and promotes MV* model development. Igor from Angular wrote a <a href="https://plus.google.com/+AngularJS/posts/aZNVhj355G2">short take on whether Angular is a MVC or MVVM</a> and his conclusion is Angular is MVW pattern where ‘W’ stands for whatever you want it to be, Controller or View Model. Sticking with Silverlight model, I will try to related to VM as we go along. Coming back to the problem at hand, what we need to display is a static ‘Hello World’ text and in our Silverlight application, we put the text directly in the view and that is what we are going to do here as well. Going by above picture all we need to do is to put ‘Hello World’ and we are done. So here is the simple HTML which displays ‘Hello World’.</p>
<blockquote><p>head&gt;      <br />&#160;&#160;&#160; &lt;title&gt;&lt;/title&gt;       <br />&lt;/head&gt;       <br />&#160;&#160;&#160; &lt;body&gt;       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;p&gt;Hello World&lt;/p&gt;       <br />&#160;&#160;&#160; &lt;/body&gt;       <br />&lt;/html&gt;</p>
</blockquote>
<p>So your question is, where is Angular? You do not need Angular since all our work is in the View. In FeF, the view is nothing more than HTML (for now).</p>
<p>If I stop the blog right here, then I am really cheating, we did not see any Angular here. So we are going to modify the problem little bit different. So here is the new problem statement.</p>
<p><strong>Problem: We are asked to create program to display a value of string variable ‘DisplayData’, where ‘DisplayData’ contains ‘Hello World’.</strong></p>
<p><strong>Silverlight: </strong></p>
<p>(You can get the full solution of Silverlight and Angular from <a title="https://www.dropbox.com/sh/nh7ihjrqlhiax02/RrkmDC-6qL" href="https://www.dropbox.com/sh/nh7ihjrqlhiax02/RrkmDC-6qL">https://www.dropbox.com/sh/nh7ihjrqlhiax02/RrkmDC-6qL</a>)</p>
<p>As you know from my previous blogs, I am a big fan of <a href="http://jounce.codeplex.com/">Jounce framework</a> for my Silverlight development. If you are not familiar with Jounce framework, don’t worry, the concepts we are going to look at should be easy to follow. If you are a hard core Silverlight developer and not using any MVVM framework, I would strongly recommend you to take a look at this framework. Anyway, Lets look at the code we would write to display ‘Hello World’ from view model. </p>
<p>View: (MainPage.XAML)</p>
<blockquote><p>&lt;Grid x:Name=&quot;LayoutRoot&quot; Background=&quot;White&quot;&gt;      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;TextBlock HorizontalAlignment=&quot;Left&quot;&#160; TextWrapping=&quot;Wrap&quot; Text=&quot;{Binding DisplayData}&quot; VerticalAlignment=&quot;Top&quot;       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Height=&quot;25&quot; Width=&quot;289&quot;/&gt;       <br />&#160;&#160;&#160; &lt;/Grid&gt;</p>
</blockquote>
<p>&#160;</p>
<p>ViewModel:</p>
<blockquote><p>public class MainPageViewModel:BaseViewModel      <br />&#160;&#160;&#160; {       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; public string DisplayData       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; {       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; get { return &quot;Hello World&quot;; }       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; }       <br />&#160;&#160;&#160; }</p>
</blockquote>
<p>Jounce has couple of ways to do the view and viewmodel association. I am using <a href="http://jounce.codeplex.com/wikipage?title=Registering%20a%20View%20Model&amp;referringTitle=Documentation">traditional explicit Export attribute</a> to mark view and viewmodel and let Jounce do the mapping. As a Silverlight developers we know how this thing work and how data binding happen so I will stop here.</p>
<p><strong>Angular:</strong></p>
<p>One of the main reason I am really attracted to Angular is the out of the box data binding. By the way <a href="http://knockoutjs.com/">Knockout framework</a> also support data binding like Silverlight. Lets start our actual Angular code. Even though it is simple Hello World, I would like to follow same MVC pattern. First and fore most, I am doing all the development in Visual Studio 2012. I will do a quick walk through on what do I do for HTML5 development.</p>
<ol>
<li>For our testing either you can download angular.js locally and use that for our coding or point directly to the minified version in the web. </li>
<li>Create a directory where you want to create the new html page. </li>
<li>Open Visual Studio and create new ‘Blank Project’. (If you do not see it, search for blank template on search it will show up). </li>
<li>Select Solution and Add existing web page and use file system and point to the empty directory you have created. </li>
<li>Create a new folder called ‘Scripts’ where we will keep all the java scripts. </li>
</ol>
<p>View: (Home.html)</p>
<blockquote><p>&lt;!DOCTYPE html&gt;      <br />&lt;html <strong>ng-app</strong>&gt;       <br />&lt;head&gt;       <br />&lt;/head&gt;       <br />&#160;&#160;&#160; &lt;body&gt;       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;div <strong>ng-controller=&quot;Controller&quot;</strong>&gt;<strong>{{</strong>DisplayData<strong>}}</strong>&lt;/div&gt;       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <em>&lt;script src=&quot;</em><a href="http://code.angularjs.org/1.0.5/angular.min.js&quot;"><em>http://code.angularjs.org/1.0.5/angular.min.js&quot;</em></a><em>&gt;&lt;/script&gt;        <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;script src=&quot;Scripts/Controller.js&quot;&gt;&lt;/script&gt;</em>       <br />&#160;&#160;&#160; &lt;/body&gt;       <br />&lt;/html&gt;</p>
<p>&#160;</p>
</blockquote>
<p>Controller: (scripts\Controller.js)</p>
<blockquote><p>function Controller($scope) {      <br />&#160;&#160;&#160; $scope.DisplayData = &quot;Hello World&quot;;       <br />}</p>
</blockquote>
<p>So there are two code, one is HTML and another one is pure java script. Lets look at the HTML first and see if we can make any sense of it.&#160; To make it simple, let us focus only on the bolded words, rest of it normal HTML. </p>
<ol>
<li><strong>ng-app</strong>: Every angular application should have one and only <a href="http://docs.angularjs.org/api/ng.directive:ngApp">ng-app</a>. When HTML is been rendered on the browser, once Angular gets the execution context, it will try to find the application scope. In our case, we have ng-app set at &lt;HTML&gt; tag that means, angular will look for angular directives in all DOM elements. We can even have the ng-app set to a simple and single &lt;DIV&gt;, then the scope is limited to the &lt;DIV&gt;. The bottom line, you need to have one ng-app.</li>
<li><strong>ng-controller</strong>: This is one of the important directive you need to know. This is the one which connects, view to the controller like the ‘[Export]’ we did in Silverlight. You can have multiple controller in a single view (HTML) but only one ng-app. In our example, we have one controller called ‘Controller’ assigned to a &lt;DIV&gt; DOM object. So everything under DIV will get evaluated with this associated controller.</li>
<li><strong>{{variable}}:</strong> The double curly braces are nothing but data binding or ‘{Binding variable}’ in Silverlight. Same as in Silverlight, when you use a Binding variable, Silverlight will look for it in the associated ViewModel, in this case, Angular will replace the variable with value from associated controller.</li>
</ol>
<p>If you are interested in how these ng- directives are processed and how you got the hello world, instead of me repeating it as third person, <a href="http://docs.angularjs.org/guide/concepts#startup">take a look at this awesome information in Angular web site</a>. </p>
<p>Next stop is controller script. This script is very simple. We have a controller object and it gets its scope as a parameter. If you remember, in the view, you can set the ng-controller to any DOM element. The $scope is suppose to cover all the DOM elements with in it. So in our example, the controller is associated with all the elements under the &lt;DIV&gt; element. So when angular process the DOM elements, it will look for DisplayData under Controller object and it find the ‘Hello World’ and populates the&#160; value. </p>
<p>If you have any questions or doubt, please send me a note and I will try to answer. When you are working through the examples, if you run into any problems, I would recommend check out Forum at Stack Overflow.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/csharprambling.wordpress.com/487/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/csharprambling.wordpress.com/487/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=csharprambling.wordpress.com&#038;blog=18899480&#038;post=487&#038;subd=csharprambling&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://csharprambling.wordpress.com/2013/03/28/silverlight-to-angular-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/11e06646d58e4efc69ef59a8da40a25c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ksunair</media:title>
		</media:content>

		<media:content url="http://csharprambling.files.wordpress.com/2013/03/clip_image0016_thumb.png" medium="image">
			<media:title type="html">clip_image001[6]</media:title>
		</media:content>

		<media:content url="http://csharprambling.files.wordpress.com/2013/03/clip_image0026_thumb.png" medium="image">
			<media:title type="html">clip_image002[6]</media:title>
		</media:content>
	</item>
		<item>
		<title>Silverlight to Angular JS</title>
		<link>http://csharprambling.wordpress.com/2013/02/05/silverlight-to-angular-js/</link>
		<comments>http://csharprambling.wordpress.com/2013/02/05/silverlight-to-angular-js/#comments</comments>
		<pubDate>Tue, 05 Feb 2013 18:34:00 +0000</pubDate>
		<dc:creator>Unni</dc:creator>
				<category><![CDATA[AngularJS]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Webstrom]]></category>
		<category><![CDATA[Silvelright]]></category>
		<category><![CDATA[WebStorm]]></category>

		<guid isPermaLink="false">https://csharprambling.wordpress.com/?p=482</guid>
		<description><![CDATA[With heavy and broken heart finally it is time for me to say good bye to one of the best technology that I worked with,&#160; ‘Silverlight’. There is always people argue about the validity and are so happy about the &#8230; <a href="http://csharprambling.wordpress.com/2013/02/05/silverlight-to-angular-js/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=csharprambling.wordpress.com&#038;blog=18899480&#038;post=482&#038;subd=csharprambling&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>With heavy and broken heart finally it is time for me to say good bye to one of the best technology that I worked with,&#160; ‘Silverlight’. There is always people argue about the validity and are so happy about the Silverlight demise. In my opinion as a developer, I really enjoyed developing apps in Silverlight. The best part of course was data binding. So simple concept that made the coding very easy and also the separation of view and view model so clean. XAML was so easy to create view without getting too much business and so on. Now we are not talking about Silverlight but the next big thing. It is without doubt HTML5, Microsoft was very quick and wise to adopt it. As the technology progress, it is time for me to make the switch as well. So lately I started poking at various options available today to make the transition into HTML5. </p>
<p>As a silverlight developer, I follow John Papa and he is been promoting Knockout JS. I saw couple of videos and did some coding with it and it looks good. From Silverlight,&#160; it is a natural transition. While Knockout is pretty good, there is another cool kid in the block and is Angular JS. Similar to Knockout, Angular also have great tutorials, decent following in forums. One of the things I was looking for when I started learning Angular of course is data binding and it is rich. I fell in love with it on my second data binding exercise. So I am going to head into HTML5 with Angular for client side development. I have not decided on the server side technologies yet. If any one have any good comments or suggestions please feel free to send me a note or command.</p>
<p>Some good references for Angular:</p>
<p>Home &#8211; <a title="http://angularjs.org/" href="http://angularjs.org/">http://angularjs.org/</a></p>
<p>Screencasts &#8211; <a href="http://www.egghead.io/">http://www.egghead.io/</a></p>
<p>Blogs &#8211; <a title="http://blog.angularjs.org/" href="http://blog.angularjs.org/">http://blog.angularjs.org/</a></p>
<p>As I move from Silverlight to Angular I found a better IDE to go with it. <a href="http://www.jetbrains.com/webstorm/">Webstrom</a> is so far the best IDE I found for angular development.</p>
<p>So my friend ‘Silverlight’ Good Bye.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/csharprambling.wordpress.com/482/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/csharprambling.wordpress.com/482/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=csharprambling.wordpress.com&#038;blog=18899480&#038;post=482&#038;subd=csharprambling&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://csharprambling.wordpress.com/2013/02/05/silverlight-to-angular-js/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/11e06646d58e4efc69ef59a8da40a25c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ksunair</media:title>
		</media:content>
	</item>
		<item>
		<title>C# to Python</title>
		<link>http://csharprambling.wordpress.com/2013/01/28/c-to-python/</link>
		<comments>http://csharprambling.wordpress.com/2013/01/28/c-to-python/#comments</comments>
		<pubDate>Mon, 28 Jan 2013 20:07:00 +0000</pubDate>
		<dc:creator>Unni</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">https://csharprambling.wordpress.com/?p=480</guid>
		<description><![CDATA[I was suppose to do a task which I could do it in any language and my obvious first choice which is purely due to comfort level is C#. I was set to do it in C# and while reading &#8230; <a href="http://csharprambling.wordpress.com/2013/01/28/c-to-python/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=csharprambling.wordpress.com&#038;blog=18899480&#038;post=480&#038;subd=csharprambling&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I was suppose to do a task which I could do it in any language and my obvious first choice which is purely due to comfort level is C#. I was set to do it in C# and while reading around one, I noticed one of the recommendation for the task is ‘language not limited to c, java or python’. This intrigued me to check out Python. I always heard lot of good things about Python so I spend an hour going through some basic language rules and semantics. At its core, it is very simple, so I decided to learn Python as a part of the process. Here are few things surprised me since I am coming from C# world.</p>
<p>Normally when you want to access an element in an array, we do it by array[index], which is straight forward. If you want to access the last element in c#, you would do this by array[array.Length – 1]. In Python, you can access it directly as array[-1], I thought it is very clever.</p>
<p>Python also have the for and while loop but what surprised me is the way of using ‘else’ in conjunction with loop. Lets say if you want to break from a loop, you will add a if condition inside the loop and break out. Now, if you want to check after loop if the loop exited normally then you add else at the end of the loop. Have a look at <a href="http://docs.python.org/2/tutorial/controlflow.html">4.4 in the this link</a>.</p>
<p>This one is not a really surprise still worth mentioning. In C# we have value type and reference type. In Python everything is reference type, but they do have mutable and immutable. Like in C# string is still immutable and the collections like list and dictionary are mutable. On the other hand if you want to create an immutable collection ‘tuple’ will help you there.</p>
<p>The next two things are more to show the power of the language. You can write code as function programming model. You can mix and match between functional and oo style if you want. For example, you can create a function and then assign a variable with a value as parameter which in turn you can use it down the line. Take a look at this <a href="http://www.ibm.com/developerworks/linux/library/l-prog/index.html">article</a>, it explains very well.</p>
<p>The last one is so far the best; in Python it is called ‘List Comprehension’. The idea is very simple, if you want to create a new collection from a collection based on some simple if then else, then you can code the full condition in a single line of code. Take a look at the question answer in the <a href="http://stackoverflow.com/questions/4406389/if-else-in-a-list-comprehension">stack overflow</a>. I know we can do the same with simple Linq or lamba, but still it is pretty cool. This is something we use more often then you think.</p>
<p>I will keep you updated on anything new I learn or if you have comments or suggestions please let me know.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/csharprambling.wordpress.com/480/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/csharprambling.wordpress.com/480/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=csharprambling.wordpress.com&#038;blog=18899480&#038;post=480&#038;subd=csharprambling&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://csharprambling.wordpress.com/2013/01/28/c-to-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/11e06646d58e4efc69ef59a8da40a25c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ksunair</media:title>
		</media:content>
	</item>
		<item>
		<title>Use PromptBox to get simple user input in Silverlight</title>
		<link>http://csharprambling.wordpress.com/2012/11/27/use-promptbox-to-get-simple-user-input-in-silverlight/</link>
		<comments>http://csharprambling.wordpress.com/2012/11/27/use-promptbox-to-get-simple-user-input-in-silverlight/#comments</comments>
		<pubDate>Tue, 27 Nov 2012 21:30:00 +0000</pubDate>
		<dc:creator>Unni</dc:creator>
				<category><![CDATA[Component One]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[C1PromtBox]]></category>

		<guid isPermaLink="false">https://csharprambling.wordpress.com/?p=477</guid>
		<description><![CDATA[When we create Silverlight application we will run into a situation where we need to get very simple input from a user apart from traditional ok or cancel. If you are after the simple ok or cancel then message box &#8230; <a href="http://csharprambling.wordpress.com/2012/11/27/use-promptbox-to-get-simple-user-input-in-silverlight/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=csharprambling.wordpress.com&#038;blog=18899480&#038;post=477&#038;subd=csharprambling&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>When we create Silverlight application we will run into a situation where we need to get very simple input from a user apart from traditional ok or cancel. If you are after the simple ok or cancel then message box serve the purpose but if you need to get some kind of data from the user as simple as a number or a string, then we do not need to create popup window or child window in Silverlight if you are using component one controls. </p>
<p>Out of the box, component provides a user control called C1PromptBox which as the name suggests, prompt the user for an input and goes on it merry way without cluttering code with complicated code. </p>
<p>Let’s look at this with simple example;</p>
<pre>C1PromptBox.Show(“Enter your age”, “Age prompt”, (s, r) =&gt;
{
If (r ==MessageBoxResult.OK)
{
//age will be in s, parse the age out
}
else
{
// user pressed cancel
}
}</pre>
<p>If you expect the last two line to execute after getting user input, then if condition has to be inside the C1PromoptBox.Show bot outside like the code shown </p>
<p>above. The proper way to write the code </p>
<p>In the above example, when the code executes it will prompt the small message box to enter the age and you parse or handle the value on the event user press ok button. </p>
<p>One thing we need to be mindful, the Show event is a delegate so if you have any instructions that need to execute after prompt is successful, it has to be inside the MessageBoxResult.OK condition not outside the show delegate since UI will take control back after show event is successful. </p>
<p>Here is a scenario; </p>
<ol>
<li>public void PromptAge() </li>
<li>{ </li>
<li>int age = 0; </li>
<li>C1PromptBox.Show(&#8230; get age); </li>
<li>if (age &lt;= 30) </li>
<li>HandleLessThanThirtyAge(); </li>
<li>} </li>
</ol>
<pre>&#160;</pre>
<p>In the above example we are expecting the dialog to be shown for user to enter the age. After the user enters the age, line 05 gets control and executes HandlesLessThanThirtyAge method if the age is less than or equal to 30. But that is not how it works. When PromptAge executes, it execute prompt box and while it wait for user to enter something, the UI thread continues and executes 05 through so age will be always 0 and it never executes the method on 06. The reason for that is, the behavior on how to handle ok or cancel is delegate inside the prompt box. So for it work properly. ie, after user enters the ages, get the age and based on the age to execute the HandleLessThanThirtyAge, we need to move the if condition inside the PromptBox delegate itself as shown below </p>
<ol>
<li>public void PromptAge() </li>
<li>{ </li>
<li>C1PromptBox.Show(&quot;msg&quot;, &quot;prompt&quot;, (s, r) =&gt; </li>
<li>{ </li>
<li>if (r == MessageBoxResult.OK) </li>
<li>{ </li>
<li>//parse age from s </li>
<li>if (age &lt;= 30) </li>
<li>HandleLessThanThirtyAge() </li>
<li>} </li>
<li>}; </li>
<li>} </li>
</ol>
<pre>One of the feature missing in this is default value for the prompt, I requested this feature with Component One and it is schedule be part of their feature release. By providing most commonly used value as default value, we could save one key stoke for the user. Once the feature is available I will update this blog.</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/csharprambling.wordpress.com/477/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/csharprambling.wordpress.com/477/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=csharprambling.wordpress.com&#038;blog=18899480&#038;post=477&#038;subd=csharprambling&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://csharprambling.wordpress.com/2012/11/27/use-promptbox-to-get-simple-user-input-in-silverlight/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/11e06646d58e4efc69ef59a8da40a25c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ksunair</media:title>
		</media:content>
	</item>
	</channel>
</rss>
