{ "id": "guide/property-binding", "title": "Property binding", "contents": "\n\n\n
Property binding in Angular helps you set values for properties of HTML elements or directives.\nWith property binding, you can do things such as toggle button functionality, set paths programmatically, and share values between components.
\nSee the
To get the most out of property binding, you should be familiar with the following:
\n\nProperty binding moves a value in one direction, from a component's property into a target element property.
\nFor more information on listening for events, see Event binding.
\nTo read a target element property or call one of its methods, see the API reference for ViewChild and ContentChild.
\nTo bind to an element's property, enclose it in square brackets, [], which identifies the property as a target property.\nA target property is the DOM property to which you want to assign a value.\nFor example, the target property in the following code is the image element's src property.
In most cases, the target name is the name of a property, even when it appears to be the name of an attribute.\nIn this example, src is the name of the <img> element property.
The brackets, [], cause Angular to evaluate the right-hand side of the assignment as a dynamic expression.\nWithout the brackets, Angular treats the right-hand side as a string literal and sets the property to that static value.
Omitting the brackets renders the string parentItem, not the value of parentItem.
To bind the src property of an <img> element to a component's property, place the target, src, in square brackets followed by an equal sign and then the property.\nThe property here is itemImageUrl.
Declare the itemImageUrl property in the class, in this case AppComponent.
colspan and colSpanlinkA common point of confusion is between the attribute, colspan, and the property, colSpan.\nNotice that these two names differ by only a single letter.
If you wrote something like this:
\nYou'd get this error:
\nAs the message says, the <td> element does not have a colspan property. This is true\nbecause colspan is an attribute—colSpan, with a capital S, is the\ncorresponding property. Interpolation and property binding can set only properties, not attributes.
Instead, you'd use property binding and write it like this:
\nAnother example is disabling a button when the component says that it isUnchanged:
Another is setting a property of a directive:
\nYet another is setting the model property of a custom component—a great way\nfor parent and child components to communicate:
\nTo disable a button's functionality depending on a Boolean value, bind the DOM disabled property to a property in the class that is true or false.
Because the value of the property isUnchanged is true in the AppComponent, Angular disables the button.
To set a property of a directive, place the directive within square brackets , such as [ngClass], followed by an equal sign and the property.\nHere, the property is classes.
To use the property, you must declare it in the class, which in this example is AppComponent.\nThe value of classes is special.
Angular applies the class special to the <p> element so that you can use special to apply CSS styles.
To set the model property of a custom component, place the target, here childItem, between square brackets [] followed by an equal sign and the property.\nHere, the property is parentItem.
To use the target and the property, you must declare them in their respective classes.
\nDeclare the target of childItem in its component class, in this case ItemDetailComponent.
For example, the following code declares the target of childItem in its component class, in this case ItemDetailComponent.
Then, the code contains an @Input() decorator with the childItem property so data can flow into it.
Next, the code declares the property of parentItem in its component class, in this case AppComponent.\nIn this example the type of childItem is string, so parentItem needs to be a string.\nHere, parentItem has the string value of lamp.
With this configuration, the view of <app-item-detail> uses the value of lamp for childItem.
Property binding can help keep content secure.\nFor example, consider the following malicious content.
\nThe component template interpolates the content as follows:
\nThe browser doesn't process the HTML and instead displays it raw, as follows.
\nAngular does not allow HTML with <script> tags, neither with interpolation nor property binding, which prevents the JavaScript from running.
In the following example, however, Angular sanitizes the values before displaying them.
\nInterpolation handles the <script> tags differently than property binding, but both approaches render the content harmlessly.\nThe following is the browser output of the sanitized evilTitle example.
Often interpolation and property binding can achieve the same results.\nThe following binding pairs do the same thing.
\nYou can use either form when rendering data values as strings, though interpolation is preferable for readability.\nHowever, when setting an element property to a non-string data value, you must use property binding.
\n