-
Notifications
You must be signed in to change notification settings - Fork 141
translate: guide/attribute-binding #818
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# Attribute binding | ||
|
||
Attribute binding in Angular helps you set values for attributes directly. | ||
With attribute binding, you can improve accessibility, style your application dynamically, and manage multiple CSS classes or styles simultaneously. | ||
|
||
<div class="alert is-helpful"> | ||
|
||
See the <live-example></live-example> for a working example containing the code snippets in this guide. | ||
|
||
</div> | ||
|
||
## Prerequisites | ||
|
||
- [Property Binding](guide/property-binding) | ||
|
||
## Syntax | ||
|
||
Attribute binding syntax resembles [property binding](guide/property-binding), but instead of an element property between brackets, you precede the name of the attribute with the prefix `attr`, followed by a dot. | ||
Then, you set the attribute value with an expression that resolves to a string. | ||
|
||
<code-example format="html" language="html"> | ||
|
||
<p [attr.attribute-you-are-targeting]="expression"></p> | ||
|
||
</code-example> | ||
|
||
<div class="alert is-helpful"> | ||
|
||
When the expression resolves to `null` or `undefined`, Angular removes the attribute altogether. | ||
|
||
</div> | ||
|
||
## Binding ARIA attributes | ||
|
||
One of the primary use cases for attribute binding is to set ARIA attributes. | ||
|
||
To bind to an ARIA attribute, type the following: | ||
|
||
<code-example header="src/app/app.component.html" path="attribute-binding/src/app/app.component.html" region="attrib-binding-aria"></code-example> | ||
|
||
<a id="colspan"></a> | ||
|
||
## Binding to `colspan` | ||
|
||
Another common use case for attribute binding is with the `colspan` attribute in tables. Binding to the `colspan` attribute helps you to keep your tables programmatically dynamic. Depending on the amount of data that your application populates a table with, the number of columns that a row spans could change. | ||
|
||
To use attribute binding with the `<td>` attribute `colspan` | ||
|
||
1. Specify the `colspan` attribute by using the following syntax: `[attr.colspan]`. | ||
1. Set `[attr.colspan]` equal to an expression. | ||
|
||
In the following example, you bind the `colspan` attribute to the expression `1 + 1`. | ||
|
||
<code-example header="src/app/app.component.html" path="attribute-binding/src/app/app.component.html" region="colspan"></code-example> | ||
|
||
This binding causes the `<tr>` to span two columns. | ||
|
||
<div class="alert is-helpful"> | ||
|
||
Sometimes there are differences between the name of property and an attribute. | ||
|
||
`colspan` is an attribute of `<td>`, while `colSpan` with a capital "S" is a property. | ||
When using attribute binding, use `colspan` with a lowercase "s". | ||
|
||
For more information on how to bind to the `colSpan` property, see the [`colspan` and `colSpan`](guide/property-binding#colspan) section of [Property Binding](guide/property-binding). | ||
|
||
</div> | ||
|
||
## What’s next | ||
|
||
- [Class & Style Binding](guide/class-binding) | ||
|
||
@reviewed 2022-05-02 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.