The following code snippets are excerpted from the Angular.io docs.
What you're referring to is the Binding target.
<img [src]="heroImageUrl">
The first div is referencing Attribute binding.
<table>
<tr><td [attr.colspan]="1 + 1">One-Two</td></tr>
</table>
The second div is referencing Class binding.
<!-- toggle the "special" class on/off with a property -->
<div [class.special]="isSpecial">The class binding is special</div>
<!-- binding to `class.special` trumps the class attribute -->
<div class="special"
[class.special]="!isSpecial">This one is not so special</div>
However, it is recommended to use NgClass
.
The last div is referencing Style binding. Believe it or not, the third div is actually legal (or at least in Angular).
<button [style.color]="isSpecial ? 'red': 'green'">Red</button>
<button [style.background-color]="canSave ? 'cyan': 'grey'" >Save</button>
However, it is recommended to use NgStyle
instead.
So, in this case, it will bind the value of the variable to the element. (Except the class
where it will evaluate whether the isDelightful
variable is true
.)
<div [attr.role]="myAriaRole"></div>
<div [class.extra-sparkle]="isDelightful"></div>
<div [style.width.px]="mySize"></div>
Here's a Stackblitz demo for you to play around with. :)