{"id":823,"date":"2018-09-04T06:18:00","date_gmt":"2018-09-04T06:18:00","guid":{"rendered":"https:\/\/staging.infragistics.com\/blogs\/?p=823"},"modified":"2025-02-26T08:16:28","modified_gmt":"2025-02-26T08:16:28","slug":"simplifying-angular-data-binding","status":"publish","type":"post","link":"https:\/\/www.infragistics.com\/blogs\/simplifying-angular-data-binding","title":{"rendered":"Simplifying Angular Data Binding to .NET Developers"},"content":{"rendered":"\n<p>At my job, I get the opportunity to talk to many .NET developers who want to learn Angular. Often, I\u2019ve seen that they bring their .NET skills and work to map that in the learning of Angular. While the effort and drive to learn is there Angular is not .NET.<\/p>\n\n\n\n<p>Since Angular is a pure JavaScript library, I\u2019ll simplify basic but important concepts of Angular to .NET developers in this post series. &nbsp;<\/p>\n\n\n\n<p>In this article, we\u2019ll learn about Data Bindings in Angular. Luckily, Data Binding in Angular is much simpler than in .NET. &nbsp;<\/p>\n\n\n\n<p>First, Let\u2019s revise some of data binding techniques in .NET. For example, in ASP.NET MVC, you do data binding using a model. View is bound&nbsp;<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>To an object<\/li>\n\n\n\n<li>To a complex object<\/li>\n\n\n\n<li>To a collection of objects<\/li>\n<\/ol>\n\n\n\n<p>Essentially, in ASP.NET MVC, you do data binding to a model class. On the other hand, in WPF, you have data binding modes available. You can set the mode of data binding in XAML, as follows:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>One-way data binding<\/li>\n\n\n\n<li>Two-way data binding<\/li>\n\n\n\n<li>One-time data binding<\/li>\n\n\n\n<li>One-way to source data binding<\/li>\n<\/ol>\n\n\n\n<p>If you are following MVVM patterns, then you might be using INotifyPropertyChanged interface to achieve two-way data binding. Therefore, there are many ways data bindings are achieved in world of .NET.<\/p>\n\n\n\n<p>&nbsp;Data binding in Angular, however, &nbsp;is much simpler.<\/p>\n\n\n\n<p>If you are extremely new in Angular, then let me introduce you to Components. In Angular applications, what you see in the browser (or elsewhere) is a component. A component &nbsp;consists of the following parts:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>A TypeScript class called Component class<\/li>\n\n\n\n<li>A HTML file called Template of the component<\/li>\n\n\n\n<li>An optional CSS file for the styling of the component<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/static.infragistics.com\/marketing\/Blogs\/Migration\/00\/00\/00\/09\/43\/8880.abc.png\" alt=\" A component \u00a0consists of the following parts\" title=\"A component \u00a0consists of the following parts\"\/><\/figure>\n\n\n\n<p>In Angular, Data Binding determines how data will flow in between Component class and Component Template.<\/p>\n\n\n\n<p>Angular provides us three types of data bindings. They are as follows:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Interpolation<\/li>\n\n\n\n<li>Property Binding<\/li>\n\n\n\n<li>Event Binding<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/static.infragistics.com\/marketing\/Blogs\/Migration\/00\/00\/00\/09\/43\/2514.IMG2.PNG\" alt=\"Angular provides us three types of data bindings. They are as follows\" title=\"provides us three types of data bindings. They are as follows\"\/><\/figure>\n\n\n\n<p>Let\u2019s see each one by one.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"interpolation\">Interpolation <\/h2>\n\n\n\n<p>Angular interpolation is one-way data binding. It is used to pass data from component class to the template. The syntax of interpolation is <strong>{{propertyname}}.<\/strong><\/p>\n\n\n\n<p>Let\u2019s say that we have component class as shown below:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">export\u00a0class\u00a0AppComponent\u00a0{\n \n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0product\u00a0=\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0title:\u00a0'Cricket\u00a0Bat',\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0price:\u00a0500\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0};\n \n\u00a0\u00a0}<\/pre>\n\n\n\n<p>We need to pass the product from the component class to the template. Keep in mind that to keep example simple, I\u2019m hard coding the value of the product object, however, in a real scenario, data could be fetched from the database using the API. &nbsp;We can display value of the product object using interpolation, as shown in the listing below:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;h1>Product&lt;\/h1>\n&lt;h2>Title\u00a0:\u00a0{{product.title}}&lt;\/h2>\n&lt;h2>Price\u00a0:\u00a0{{product.price}}&lt;\/h2><\/pre>\n\n\n\n<p>Using interpolation, data is passed from the component class to the template. Ideally, whenever the value of the product object is changed, the template will be updated with the updated value of the product object.<\/p>\n\n\n\n<p>In Angular, there is something called <strong>ChangeDetector<\/strong> Service, which makes sure that value of property in the component class and the template are in sync with each other.<\/p>\n\n\n\n<p>Therefore, if you want to display data in Angular, you should use interpolation data binding.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"property-binding\">Property Binding <\/h2>\n\n\n\n<p>Angular provides you with a second type of binding called \u201cProperty Binding\u201d. The syntax of property binding is the square bracket []. It allows to set the property of HTML elements on atemplate with the property from the component class. <strong><u>&nbsp;<\/u><\/strong><\/p>\n\n\n\n<p>So, let\u2019s say that you have a component class like the one below:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">export\u00a0class\u00a0AppComponent\u00a0{\n\u00a0\u00a0\u00a0btnHeight\u00a0=\u00a0100;\n\u00a0\u00a0\u00a0btnWidth\u00a0=\u00a0100;\n}<\/pre>\n\n\n\n<p>Now, you can set height and width properties of a button on template with the properties of the component class using the property binding.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;button\u00a0\n\u00a0\u00a0[style.height.px]\u00a0=\u00a0'btnHeight'\n\u00a0\u00a0[style.width.px]\u00a0=\u00a0'btnWidth'\u00a0>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Add\u00a0Product\n&lt;\/button\u00a0><\/pre>\n\n\n\n<p>Angular Property Binding is used to set the property of HTML Elements with the properties of the component class. You can also set properties of other HTML elements &nbsp;like image, list, table, etc. &nbsp;Whenever the property\u2019s value in the component class &nbsp;changes, the HTML element property will be updated in the property binding.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"event-binding\">Event Binding <\/h2>\n\n\n\n<p>Angular provides you third type of binding to capture events raised on template in a component class. For instance, there\u2019s a button on the component template and, on click of the button, you want to call a function in component class. You can do this using Event Binding. The syntax behind Event Binding is <strong>(eventname).<\/strong><\/p>\n\n\n\n<p>For this example, you might have a component class like this:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">export\u00a0class\u00a0AppComponent\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0addProduct()\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0console.log('add\u00a0product');\n\u00a0\u00a0\u00a0\u00a0\u00a0}\n\u00a0}<\/pre>\n\n\n\n<p>&nbsp;You want to call addProduct function on the click of the button on the template. You can do this using event binding:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;h1>Product&lt;\/h1>\n&lt;button\u00a0(click)='addProduct()'>\n\u00a0\u00a0\u00a0\u00a0Add\u00a0Product\n&lt;\/button><\/pre>\n\n\n\n<p>You can do event binding with all events of a HTML elements which is part of Angular ngZone. You can learn more about it <span><a href=\"https:\/\/angular.io\/api\/core\/NgZone\" rel=\"noopener\">here<\/a>.<\/span><\/p>\n\n\n\n<p>Angular provides you these three bindings. In event binding, data flows from template to class and, in property binding and interpolation, data flows from class to template.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/static.infragistics.com\/marketing\/Blogs\/Migration\/00\/00\/00\/09\/43\/2514.image3.png\" alt=\" in property binding and interpolation, data flows from class to template\" title=\"in property binding and interpolation, data flows from class to template\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"two-way-data-binding\">Two-Way Data Binding <\/h2>\n\n\n\n<p>Angular does not have built-in two-way data binding, however, by combining Property Binding and Event Binding, you can achieve Two-Way Data Binding.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/static.infragistics.com\/marketing\/Blogs\/Migration\/00\/00\/00\/09\/43\/5228.tr.PNG\" alt=\" Two-Way Data Binding\" title=\"Two-Way Data Binding\"\/><\/figure>\n\n\n\n<p>Angular provides us a directive, <strong>ngModel,<\/strong> to achieve two-way data binding, and It\u2019s very easy to use. First, import FormsModule and then you can create two-way data binding:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">export\u00a0class\u00a0AppComponent\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0name\u00a0=\u00a0'foo';\n\u00a0}<\/pre>\n\n\n\n<p>&nbsp;We can two-way data bind the name property with an input box:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;input\u00a0type=\"text\"\u00a0[(ngModel)]='name'\u00a0\/>\n\n&lt;h2>{{name}}&lt;\/h2><\/pre>\n\n\n\n<p>As you see, &nbsp;we are using [(ngModel)] to create two-way data binding in between input control and name property. Whenever a user changes the value of the input box, the name property will be updated and vice versa.<\/p>\n\n\n\n<p>As a .NET developer, now you might have realized that data binding in Angular is much simpler, and all you need to know is four syntaxes. I hope you find this post useful and, in further posts, we will cover other topics of Angular. &nbsp;<\/p>\n\n\n\n<p><span>&nbsp;<\/span><span>If you like this post, please share it. Also, if you have not checked out&nbsp;<\/span><a href=\"\/products\/ignite-ui-angular\">Infragistics Ignite UI for Angular Components<\/a><span>, be sure to do so! They have 30+ material based Angular components to help you code web apps faster.<\/span><\/p>\n\n\n\n<p><a href=\"\/products\/ignite-ui-angular\/getting-started\" rel=\"noopener noreferrer\" target=\"_blank\"><span><\/span><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Handling Angular data binding may be time consuming and sometimes even too complex. This blog post will show you how to simplify the process effectively.<\/p>\n","protected":false},"author":65,"featured_media":1572,"comment_status":"publish","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-823","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-angular"],"_links":{"self":[{"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/posts\/823","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/users\/65"}],"replies":[{"embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/comments?post=823"}],"version-history":[{"count":3,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/posts\/823\/revisions"}],"predecessor-version":[{"id":2403,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/posts\/823\/revisions\/2403"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/media\/1572"}],"wp:attachment":[{"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/media?parent=823"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/categories?post=823"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/tags?post=823"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}