From 6276bed4abae623ce25ea21e0ee6a6ff7eda6738 Mon Sep 17 00:00:00 2001 From: Virginia Dooley Date: Thu, 14 Apr 2022 22:05:34 +0000 Subject: [PATCH] docs: updated Property binding doc (#45897) Refined and edited as a task-based document with the assistance of the SME Add missing prerequisites and what's next sections PR Close #45897 --- aio/content/guide/property-binding.md | 191 ++++++-------------------- 1 file changed, 44 insertions(+), 147 deletions(-) diff --git a/aio/content/guide/property-binding.md b/aio/content/guide/property-binding.md index 1ea735a3a62..36c23448079 100644 --- a/aio/content/guide/property-binding.md +++ b/aio/content/guide/property-binding.md @@ -1,8 +1,6 @@ - # Property binding -Property binding in Angular helps you set values for properties of HTML elements or directives. -Use property binding to do things such as toggle button functionality, set paths programmatically, and share values between components. +Property binding in Angular helps you set values for properties of HTML elements or directives. Use property binding to do things such as toggle button functionality, set paths programmatically, and share values between components.
@@ -12,11 +10,9 @@ See the for a working example containing the code ## Prerequisites -To get the most out of property binding, you should be familiar with the following: - -* [Basics of components](guide/architecture-components) -* [Basics of templates](guide/glossary#template) -* [Binding syntax](guide/binding-syntax) +* [Basics of components](guide/architecture-components) +* [Basics of templates](guide/glossary#template) +* [Binding syntax](guide/binding-syntax) ## Understanding the flow of data @@ -32,178 +28,79 @@ To read a target element property or call one of its methods, see the API refere ## Binding to a property -To bind to an element's property, enclose it in square bracket \(`[]`\) characters, which identifies the property as a target property. -A target property is the DOM property to which you want to assign a value. -For example, the target property in the following code is the image element's `src` property. +To bind to an element's property, enclose it in square brackets, '[]', which identifies the property as a target property. - +A target property is the DOM property to which you want to assign a value. + +To assign a value to a target property for the image element's 'src' property, type the following code: + + In most cases, the target name is the name of a property, even when it appears to be the name of an attribute. -In this example, `src` is the name of the `` element property. -The square bracket \(`[]`\) characters cause Angular to evaluate the right-hand side of the assignment as a dynamic expression. +In this example, 'src' is the name of the '' element property. + +The brackets, '[]', cause Angular to evaluate the right-hand side of the assignment as a dynamic expression. + Without the brackets, Angular treats the right-hand side as a string literal and sets the property to that static value. - +To assign a string to a property, type the following code: -Omitting the brackets renders the string `parentItem`, not the value of `parentItem`. + + +Omitting the brackets renders the string 'parentItem', not the value of 'parentItem'. ## Setting an element property to a component property value -To bind the `src` property of an `` element to a component's property, place the target, `src`, in square brackets followed by an equal sign and then the property. -The property here is `itemImageUrl`. +To bind the ‘src’ property of an '' element to a component's property, place the target, ‘src', in square brackets followed by an equal sign and then the property. - +Using the property 'itemImageUrl', type the following code: -Declare the `itemImageUrl` property in the class, in this case `AppComponent`. + - +Declare the 'itemImageUrl' property in the class, in this case 'AppComponent'. - + -#### `colspan` and `colSpan` +{@a colspan} -A common point of confusion is between the attribute, `colspan`, and the property, `colSpan`. -Notice that these two names differ by only a single letter. +#### 'colspan' and 'colSpan' -If you wrote something like this: +A common point of confusion is between the attribute, 'colspan', and the property, 'colSpan'. Notice that these two names differ by only a single letter. - +To use property binding using colSpan, type the following: -<tr><td colspan="{{1 + 1}}">Three-Four</td></tr> + - +To disable a button when the component says that it 'isUnchanged', type the following: -You'd get this error: + - +To set a property of a directive, type the following: -Template parse errors: -Can't bind to 'colspan' because it isn't a known built-in property + - +To set the model property of a custom component for parent and child components to communicated, type the following: -As the message says, the `` element does not have a `colspan` property. -This is true because `colspan` is an attribute —`colSpan`, with a capital `S`, is the corresponding property. -Interpolation and property binding can set only *properties*, not attributes. + -Instead, you'd use property binding and write it like this: - - - -Another example is disabling a button when the component says that it `isUnchanged`: - - - -Another is setting a property of a directive: - - - -Yet another is setting the model property of a custom component —a great way for parent and child components to communicate: - - ## Toggling button functionality -To 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`. +To 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. +Because the value of the property 'isUnchanged' is 'true' in the 'AppComponent', Angular disables the button. - - -## Setting a directive property - -To set a property of a directive, place the directive within square brackets , such as `[ngClass]`, followed by an equal sign and the property. -Here, the property is `classes`. - - - -To use the property, you must declare it in the class, which in this example is `AppComponent`. -The value of `classes` is `special`. - - - -Angular applies the class `special` to the `

` element so that you can use `special` to apply CSS styles. - -## Bind values between components - -To set the model property of a custom component, place the target, here `childItem`, between square bracket \(`[]`\) characters followed by an equal sign and the property. -Here, the property is `parentItem`. - - - -To use the target and the property, you must declare them in their respective classes. - -Declare 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`. -In this example the type of `childItem` is `string`, so `parentItem` needs to be a string. -Here, `parentItem` has the string value of `lamp`. - - - -With this configuration, the view of `` uses the value of `lamp` for `childItem`. - -## Property binding and security - -Property binding can help keep content secure. -For example, consider the following malicious content. - - - -The component template interpolates the content as follows: - - - -The browser doesn't process the HTML and instead displays it raw, as follows. - - - -"Template <script>alert("evil never sleeps")</script> Syntax" is the interpolated evil title. - - - -Angular does not allow HTML with `