{"id":34564,"date":"2016-05-23T15:29:44","date_gmt":"2016-05-23T09:59:44","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=34564"},"modified":"2017-09-12T12:43:55","modified_gmt":"2017-09-12T07:13:55","slug":"angular-2-component-dataflow","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/angular-2-component-dataflow\/","title":{"rendered":"Angular 2 component dataflow"},"content":{"rendered":"<p>Angular 2 leverages the power of web components. If you are familiar with Angular 1, you know the term directives. Actually, directives are still here in Angular 2. Components are a combination of Directives, controllers, and scope of Angular 1.<\/p>\n<h3>Components<\/h3>\n<p>Angular 2 app has at least one root component. Components are the basic building blocks of <a title=\"Angular Development\" href=\"http:\/\/www.tothenew.com\/front-end-angularjs-development\">angular application<\/a> that encapsulate the template, data, and behavior of the view through associate decorator-style directives with it, In the following example, there are two decorator functions associated with AppComponent. Decorator function takes a metadata object, @Component decorator to set selector and @View decorator to set template URL.<\/p>\n<p>[js]<\/p>\n<p>@Component({<br \/>\n     selector: &quot;my-app&quot;<br \/>\n })<br \/>\n @View({<br \/>\n     templateUrl: &quot;AppComponent.html&quot;<br \/>\n })<br \/>\n class AppComponent() {<br \/>\n     constructor () {<br \/>\n     }<br \/>\n }<\/p>\n<p>[\/js]<\/p>\n<p><a href=\"http:\/\/www.tothenew.com\/blog\/deep-dive-into-angular-directives\/\">Angular 2 make directives<\/a> class-based structure, its unify Directives and Controllers into the Component model.<\/p>\n<h3>INPUT AND OUTPUT PROPERTIES<\/h3>\n<p>A component has input and output properties, that can be passed to the @Component decorator to implement the downward and upward flow of data: \u201cinputs\u201d and \u201coutputs.\u201d.\u201cInputs\u201d property can set on a component whereas \u201coutputs\u201d property identifies the events a component can fire to send information up the hierarchy to its parent.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-34672\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/05\/WhatsApp-Image-20160515.jpg\" alt=\"WhatsApp-Image-20160515\" width=\"1280\" height=\"587\" \/>To better understand downward and upward flow of data, let\u2019s take an example of product rating and performance.we are going to create two components app component (Parent component) and rating component(Child component).<\/p>\n<h4>Rating Component:<\/h4>\n<p>[js]<br \/>\nimport {Component, View, NgFor, NgClass, EventEmitter} from &#8216;@angular\/core&#8217;;<br \/>\n@Component({<br \/>\n    directives: [NgFor, NgClass]<br \/>\n})<\/p>\n<p>export class RatingComponent{<br \/>\n\u00a0private range:Array&lt;number&gt; = [1,2,3,4,5];<br \/>\n\u00a0private performance:Array&lt;string&gt; = [&quot;Very poor&quot;,&quot;poor&quot;,&quot;OK&quot;,&quot;good&quot;,&quot;Very good&quot;];<br \/>\n\u00a0private rate:number;<br \/>\n\u00a0private productPerformance:EventEmitter = new EventEmitter();<br \/>\n update(value) {<br \/>\n    \u00a0this.rate = value;<br \/>\n\u00a0    this.productPerformance.next(this.performance[value-1]);<br \/>\n    }<br \/>\n\u00a0}<br \/>\n[\/js]<\/p>\n<p>Rating component (Child Component) has input and output properties, that are passed to the @Component decorator to implement the downward and upward flow of data.\u201cInputs\u201d property define that we will take input from App component (Parent Component) and store it in &#8216;rate&#8217; variable whereas \u201coutputs\u201d property defines that an EventEmitter object \u2018rateChange\u2019 that fire an event to sent data to the parent component.<\/p>\n<h4>App Component:<\/h4>\n<p>[js]<\/p>\n<p>import {Component, View, bootstrap , CORE_DIRECTIVES} from &#8216;@angular2\/angular2&#8217;;<br \/>\nimport {RatingComponent} from &#8216;.\/rating.component&#8217;;<\/p>\n<p>@Component({<br \/>\n    selector: &#8216;my-app&#8217;,<br \/>\n    template: `<br \/>\n       &lt;rating [rate]=&quot;4&quot;<br \/>\n        (rate-change)=&quot;productPerformance=$event&quot; &gt;<br \/>\n        Product performance:{{productPerformance}}<br \/>\n        `,<br \/>\n    directives: [RatingComponent, CORE_DIRECTIVES]<br \/>\n})<br \/>\nexport class App{<br \/>\n  private rate: number;<br \/>\n  private productPerformance: number;<br \/>\n}<\/p>\n<p>[\/js]<\/p>\n<p>In App Component (Parent Component) we are sending the value of product rating to Rating Component (Child Component) by attribute [rate]=&#8221;3&#8243; and bind an EventEmmiter \u2018rate-change\u2019 to get product performance from Rating Component (child component) when using change product rating. You can try out the code at\u00a0http:\/\/plnkr.co\/edit\/xKaFd4jRid9mBVrt8mD8?p=preview<br \/>\nOutput:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-34701\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/05\/Screenshot-from-2016-05-14-204044.png\" alt=\"Screenshot from 2016-05-14 20:40:44\" width=\"563\" height=\"169\" \/><\/p>\n<h3>Conclusion:<\/h3>\n<p>In this blog, we\u2019ve looked at Angular 2 components data-flow by using \u2018inputs\u2019 and \u2018output\u2019 properties and how they relate and how you pass data to them and how to get data back out. Overall this is not a bad way to build an application. But as your <a title=\"AngularJS App\" href=\"http:\/\/www.tothenew.com\/blog\/building-intuitive-frontend-interfaces-with-angularjs\/\">Angular applications<\/a> grow, dependencies between components and the data they render can become cumbersome. It can make reasoning about your application difficult and rely on data mutation and dirty checking can make testing harder.<\/p>\n<p>Read Further on AngularJS Series<\/p>\n<ul>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-a-write-less-do-more-javascript-framework\/\">AngularJS :\u00a0A \u201cWrite Less Do More\u201d JavaScript Framework<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-updating-a-label-dynamically-with-user-input\/\">AngularJS :\u00a0Updating a Label Dynamically with User Input<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-adding-items-to-a-javascript-list-and-updating-corresponding-dom-dynamically\/\">AngularJS :\u00a0Adding Items to a JavaScript List and updating corresponding DOM Dynamically<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-text-suggestions\/\">AngularJS :\u00a0Text Suggestions<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-sorting-objects-on-various-attributes\/\">AngularJS :\u00a0Sorting objects on various attributes<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-fetching-data-from-the-server\/\">AngularJS :\u00a0Fetching data from the server<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-multiple-views-layout-template-and-routing\/\">AngularJS :\u00a0Multiple Views, Layout\u00a0Template\u00a0and Routing<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-implementing-routes-and-multiple-views\/\">AngularJS :\u00a0Implementing Routes And Multiple Views<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/building-intuitive-frontend-interfaces-with-angularjs\/\">Building Intuitive Frontend Interfaces with AngularJS<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Angular 2 leverages the power of web components. If you are familiar with Angular 1, you know the term directives. Actually, directives are still here in Angular 2. Components are a combination of Directives, controllers, and scope of Angular 1. Components Angular 2 app has at least one root component. Components are the basic building [&hellip;]<\/p>\n","protected":false},"author":924,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":3},"categories":[1439,3429],"tags":[3393,3394,3395,3348,3955,4697],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/34564"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/924"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=34564"}],"version-history":[{"count":1,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/34564\/revisions"}],"predecessor-version":[{"id":51796,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/34564\/revisions\/51796"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=34564"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=34564"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=34564"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}