{"id":3555,"date":"2018-01-12T19:59:21","date_gmt":"2018-01-12T19:59:21","guid":{"rendered":"http:\/\/pensemosweb.com.mx\/index.php\/2018\/01\/12\/enlazado-propiedades-property-binding-angular\/"},"modified":"2024-03-08T09:51:22","modified_gmt":"2024-03-08T15:51:22","slug":"enlazado-propiedades-property-binding-angular","status":"publish","type":"post","link":"https:\/\/pensemosweb.com\/en\/enlazado-propiedades-property-binding-angular\/","title":{"rendered":"How to bind properties between components? Angular Property Binding"},"content":{"rendered":"<p>Property Binding is a quick and easy way to define a property to a component of an Angular application and update this property if a change in the origin of the information is detected.<\/p>\n<p>Property binding can be defined to a native HMTL element, a component or a directive. To define a property, use <em>Template expressions<\/em>I recommend that you read this post. <a href=\"https:\/\/pensemosweb.com\/en\/como-usar-expresiones-interpolacion-property-binding\/\">How to use Expressions in Angular Interpolation and Property Binding?<\/a>.<\/p>\n<p>One of the simplest examples is property binding <code>src<\/code>, in the section <strong>Secure content<\/strong>From this post you can see the example in action with plunker:<\/p>\n<pre><code class=\"language-typescript\">&lt;img [src]=&quot;persona.photoURL&quot;&gt;\n<\/code><\/pre>\n<p>The label <code>img<\/code>also has an attribute <code>src<\/code>, but here it is worth pointing out that the properties and attributes of an element are not the same. Property Binding applies to the properties themselves, not the attributes.<\/p>\n<p>Attributes are sometimes used to reflect the state of an element or component, but an attribute is useful for declaring initial element information in HTML code, a property is part of the element instance.<\/p>\n<p>To prove that they are different, if you want to obtain the value of an attribute, use <code>element.getAttribute(attributeName)<\/code>, to obtain the value of a property we use <code>element.property<\/code>.<\/p>\n<p>To define an attribute, use <code>element.setAttribute(attributeName, value)<\/code>, to define a property we use <code>element.property = value<\/code>.<\/p>\n<p>Attributes are stored in a property <code>element.attributes<\/code>of the element or component instance. To understand this about attributes and properties, I recommend the section <a href=\"https:\/\/pensemosweb.com\/en\/como-crear-un-componente-web-elementos-html-personalizados\/#sincronizacindeatributosypropiedades\"><strong>synchronization of attributes and properties<\/strong><\/a>.<\/p>\n<p>Now let&#039;s see how we can enable and disable a <code>&lt;input&gt;<\/code>through property <code>disabled<\/code>:<\/p>\n<pre><code class=\"language-typescript\">&lt;input [disabled]=\"isDisabled\" value=\"Hola\"\/&gt;\n<\/code><\/pre>\n<p>There is another notation to link properties, <code>bind-property=&quot;expression&quot;<\/code>:<\/p>\n<pre><code class=\"language-typescript\">&lt;input bind-disabled=\"isDisabled\" value=\"Hola\"\/&gt;\n<\/code><\/pre>\n<p>Another interesting example is defining the value <code>class<\/code>of an element through the directive <code>ngClass<\/code>:<\/p>\n<pre><code class=\"language-typescript\">&lt;p [ngclass]=&quot;clases&quot;&gt;&lt;\/p&gt;\n<\/code><\/pre>\n<p>Here the question is, <strong>How does angular know that <code>ngClass<\/code>Is it a directive and not a property?<\/strong><\/p>\n<p>In another publication we will see how to create a directive, for the moment just take into account that the way in which directives are created allows Angular to check whether it is a directive or not. Angular before trying to do a property binding checks if a property exists <code>ngClass<\/code>in the directive or in the element that is being applied. In the above case, the native elements do not have the property <code>ngClass<\/code>, html elements have the property <code>className<\/code>and <code>classList<\/code>, but in this case we can say that <code>[ngClass]<\/code>It is a directive.<\/p>\n<h2 id=\"contenidoseguro\">Secure content<\/h2>\n<p>Angular does its job in keeping your code secure, here is an example using property binding:<\/p>\n<pre><code class=\"language-typescript\">caution: string = &#039;  &#039;;\n<\/code><\/pre>\n<p><iframe src=\"https:\/\/stackblitz.com\/github\/jaimeCervantes\/angular-property-binding?ctl=1&amp;embed=1&amp;file=src\/app\/app.component.ts&amp;view=both&amp;#full\"><\/iframe><\/p>\n<h2 id=\"comunicacinentrecomponentes\">Communication between components<\/h2>\n<p>The binding of properties is very useful in the communication from Parent to child of components, let&#039;s see a concrete example, we have the parent component <code>pb-person-list<\/code>, which contains a list of child components of type <code>pb-person<\/code>.<\/p>\n<ul>\n<li>Parent component:\n<ul>\n<li><code>src\/app\/pb-person-list\/pb-person-list.component.ts<\/code><\/li>\n<li><code>src\/app\/pb-person-list\/pb-person-list.component.html<\/code><\/li>\n<\/ul>\n<\/li>\n<li>Child component:\n<ul>\n<li><code>src\/app\/pb-person-list\/pb-person.component.ts<\/code><\/li>\n<li><code>src\/app\/pb-person-list\/pb-person.component.html<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>The parent component contains an array of people, this array is bound through the directive <code>*ngFor<\/code>and <em>Property Binding<\/em>with the property <code>person<\/code>of the child component.<\/p>\n<p>Here is something very important, so that the child component can perform the <code>Property Linking<\/code>, the property is defined <code>person<\/code>with the decorator <code>@Input(&#039;alias&#039;)<\/code>, to himself the property <code>person<\/code>inside the component it is used as <code>person<\/code>, but from the outside it is bound with <code>[person]=&quot;expression&quot;&#039;<\/code>, this is because of the alias that we defined in <code>@Input(&#039;person) person: Object<\/code>. The alias is optional, if it does not have it, then it is bound with <code>[person]=&quot;expression&quot;<\/code><\/p>\n<p><iframe src=\"https:\/\/stackblitz.com\/github\/jaimeCervantes\/angular-property-binding-parent-child?ctl=1&amp;embed=1&amp;file=src\/app\/app.component.ts&amp;view=both&amp;#full\"><\/iframe><\/p>","protected":false},"excerpt":{"rendered":"<p>        Property Binding es una forma f\u00e1cil y r\u00e1pida en que se puede definir una propiedad a un componente y actualizar esta propiedad si se detecta un cambio.<\/p>","protected":false},"author":2,"featured_media":3753,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[23],"tags":[40],"class_list":["post-3555","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-angular","tag-angular"],"_links":{"self":[{"href":"https:\/\/pensemosweb.com\/en\/wp-json\/wp\/v2\/posts\/3555","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pensemosweb.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pensemosweb.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pensemosweb.com\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/pensemosweb.com\/en\/wp-json\/wp\/v2\/comments?post=3555"}],"version-history":[{"count":1,"href":"https:\/\/pensemosweb.com\/en\/wp-json\/wp\/v2\/posts\/3555\/revisions"}],"predecessor-version":[{"id":3627,"href":"https:\/\/pensemosweb.com\/en\/wp-json\/wp\/v2\/posts\/3555\/revisions\/3627"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pensemosweb.com\/en\/wp-json\/wp\/v2\/media\/3753"}],"wp:attachment":[{"href":"https:\/\/pensemosweb.com\/en\/wp-json\/wp\/v2\/media?parent=3555"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pensemosweb.com\/en\/wp-json\/wp\/v2\/categories?post=3555"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pensemosweb.com\/en\/wp-json\/wp\/v2\/tags?post=3555"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}