2020-09-23 11:02:04 -04:00
# Two-way binding
2020-04-28 16:26:58 -04:00
2020-09-23 11:02:04 -04:00
Two-way binding gives components in your application a way to share data.
2020-11-26 14:14:26 -05:00
Use two-way binding to listen for events and update values simultaneously between parent and child components.
2020-04-28 16:26:58 -04:00
< div class = "alert is-helpful" >
See the < live-example > < / live-example > for a working example containing the code snippets in this guide.
< / div >
2020-09-23 11:02:04 -04:00
## Prerequisites
2020-04-28 16:26:58 -04:00
2020-09-23 11:02:04 -04:00
To get the most out of two-way binding, you should have a basic understanding of the following concepts:
2020-04-28 16:26:58 -04:00
2020-09-23 11:02:04 -04:00
* [Property binding ](guide/property-binding )
* [Event binding ](guide/event-binding )
* [Inputs and Outputs ](guide/inputs-outputs )
2020-04-28 16:26:58 -04:00
2020-09-23 11:02:04 -04:00
< hr >
2020-04-28 16:26:58 -04:00
2020-09-23 11:02:04 -04:00
Two-way binding combines property binding with event binding:
2020-04-28 16:26:58 -04:00
2020-09-23 11:02:04 -04:00
* [Property binding ](guide/property-binding ) sets a specific element property.
* [Event binding ](guide/event-binding ) listens for an element change event.
2020-04-28 16:26:58 -04:00
2020-09-23 11:02:04 -04:00
## Adding two-way data binding
2020-04-28 16:26:58 -04:00
2020-09-23 11:02:04 -04:00
Angular's two-way binding syntax is a combination of square brackets and parentheses, `[()]` .
The `[()]` syntax combines the brackets of property binding, `[]` , with the parentheses of event binding, `()` , as follows.
< code-example path = "two-way-binding/src/app/app.component.html" header = "src/app/app.component.html" region = "two-way-syntax" > < / code-example >
## How two-way binding works
For two-way data binding to work, the `@Output()` property must use the pattern, `inputChange` , where `input` is the name of the `@Input()` property.
For example, if the `@Input()` property is `size` , the `@Output()` property must be `sizeChange` .
2020-04-28 16:26:58 -04:00
2020-09-23 11:02:04 -04:00
The following `sizerComponent` has a `size` value property and a `sizeChange` event.
The `size` property is an `@Input()` , so data can flow into the `sizerComponent` .
2021-07-21 01:01:39 -04:00
The `sizeChange` event is an `@Output()` , which lets data flow out of the `sizerComponent` to the parent component.
2020-04-28 16:26:58 -04:00
2020-09-23 11:02:04 -04:00
Next, there are two methods, `dec()` to decrease the font size and `inc()` to increase the font size.
These two methods use `resize()` to change the value of the `size` property within min/max value constraints, and to emit an event that conveys the new `size` value.
< code-example path = "two-way-binding/src/app/sizer/sizer.component.ts" region = "sizer-component" header = "src/app/sizer.component.ts" > < / code-example >
The `sizerComponent` template has two buttons that each bind the click event to the `inc()` and `dec()` methods.
When the user clicks one of the buttons, the `sizerComponent` calls the corresponding method.
Both methods, `inc()` and `dec()` , call the `resize()` method with a `+1` or `-1` , which in turn raises the `sizeChange` event with the new size value.
2020-04-28 16:26:58 -04:00
< code-example path = "two-way-binding/src/app/sizer/sizer.component.html" header = "src/app/sizer.component.html" > < / code-example >
2020-09-23 11:02:04 -04:00
In the `AppComponent` template, `fontSizePx` is two-way bound to the `SizerComponent` .
2020-04-28 16:26:58 -04:00
2020-09-23 11:02:04 -04:00
< code-example path = "two-way-binding/src/app/app.component.html" header = "src/app/app.component.html" region = "two-way-1" > < / code-example >
2020-04-28 16:26:58 -04:00
2020-09-23 11:02:04 -04:00
In the `AppComponent` , `fontSizePx` establishes the initial `SizerComponent.size` value by setting the value to `16` .
2020-04-28 16:26:58 -04:00
< code-example path = "two-way-binding/src/app/app.component.ts" header = "src/app/app.component.ts" region = "font-size" > < / code-example >
2020-09-23 11:02:04 -04:00
Clicking the buttons updates the `AppComponent.fontSizePx` .
The revised `AppComponent.fontSizePx` value updates the style binding, which makes the displayed text bigger or smaller.
2020-04-28 16:26:58 -04:00
2020-09-23 11:02:04 -04:00
The two-way binding syntax is shorthand for a combination of property binding and event binding.
The `SizerComponent` binding as separate property binding and event binding is as follows.
2020-04-28 16:26:58 -04:00
2020-09-23 11:02:04 -04:00
< code-example path = "two-way-binding/src/app/app.component.html" header = "src/app/app.component.html (expanded)" region = "two-way-2" > < / code-example >
2020-04-28 16:26:58 -04:00
2020-09-23 11:02:04 -04:00
The `$event` variable contains the data of the `SizerComponent.sizeChange` event.
2020-04-28 16:26:58 -04:00
Angular assigns the `$event` value to the `AppComponent.fontSizePx` when the user clicks the buttons.
2020-09-23 11:02:04 -04:00
< div class = "callout is-helpful" >
2020-04-28 16:26:58 -04:00
2020-09-23 11:02:04 -04:00
< header > Two-way binding in forms< / header >
2020-04-28 16:26:58 -04:00
2021-07-21 01:01:39 -04:00
Because no built-in HTML element follows the `x` value and `xChange` event pattern, two-way binding with form elements requires `NgModel` .
2020-09-23 11:02:04 -04:00
For more information on how to use two-way binding in forms, see Angular [NgModel ](guide/built-in-directives#ngModel ).
< / div >