Bindings
Last updated
Last updated
Bindings creates a live connection between Template (UI, DOM element) elements and the Model (the component instance to which the template belongs).
The Change Detection
algorithm is responsible for keeping the View and Model in sync.
{{ }}
Refers to embedding expressions into marked up text.
You can type any expressions that can be resolved or converted to a string, not only variable names.
Helps you set values for properties of native HTML elements or Angular's Directives.
It moves a value in one direction.
The brackets []
, cause Angular to evaluate the right-ahdn side of the assignment as a dynamic expression. (Without it, Angular treats it as a string literal)
[attr.]
Helps you set values for attrubutes directly.
Resembles property binding, but instead of an element property between brackets, you precede the name of the attribute with the prefix attr
.
[class.]
Use class bindings to conditionally add and remove CSS class on elements based on whether the bound value is true
or false
.
Angular does not guarantee any specific order of CSS classes on rendered elements.
Remember that when working with Objects
, Arrays
, Map
or Set
, the identity of the object must change for Angular to recognize the update.
Mutation of these objects will not trigger Change Detection
.
classExpression
can be:
string
: With one or more space-delimited class names.
{}
: An object with class names as keys and truthy or falsy expressions as values determining if the given class should be applied or not.
[]
: An array of class names as strings
.
[style.]
Remember that when working with Objects
, Arrays
, Map
or Set
, the identity of the object must change for Angular to recognize the update.
Mutation of these objects will not trigger Change Detection
.
You may add a unit extension like em
or %
.
styleExpression
can be:
string
: A string list of styles like:
width: 100px; height: 100px; background-color: cornflowerblue;
{}
: An object with style names as the keys an styles values as the values:
{width: '100px', height: '100px', backgroundColor: 'cornflowerblue'}
Binding arrays
is not supported.
Lets you listen for and respond to user actions such as keystrokes
, mouse movements
, clicks
and touches
.
The syntax consists of a target event name within ()
to the left of an equal sign, and a quoted template statement to the right.
To determine an event target, Angular checks if the name of the target event matches an event property of a known directive.
$event
ArgumentIn every template event listener, Angular provides a variable named $event
that contains a reference to the event object.
You may specify the key or code that you would like to bind to.
By default event bindings will assume you will use the key
field of the keyboard event. You can also use the code
field.
The code
is more specific than the key
.
Combinations of keys can be separated by a .
.
Depending on the OS, some key combinations might create special characters instead of the key combination you would expect.
Use code
keyboard event field to get the correct behavior.
It is a shorthand to simultaneously bind a value into an element, while also giving that element the ability to propagate changes back through this binding.
#Broken link in forms
Use model()
signal function instead.
For it to work, the @Output
property must use the pattern, inputChange
, where input
is the name of the @Input
property.
For example:
If @Input size!: number | string
, so data can flow into the Component.
Then @Output sizeChange = new EventEmiter<number>()
, which lets data flow out of the Component, to it's parent.