Angular 2 Two Way Binding Tutorial - Loading..
Style: Blog or IDE

Angular 2 Two Way Data Binding Tutorial

Combine the square brackets of property binding and the parentheses of event binding to create two-way binding with ngModel.

Quick Tip

In the text below, when you see a word or phrase with this color, click on it to highlight the source code.

Also, take a look at the IDE style to see this tutorial in wide-screen mode. You can always switch back using the Style link at the top of the page.

Show me the IDE style

Hello World

We start with a Hello World example to familiarise ourselves with the concept of two-way data binding.

Two-way data binding combines the square brackets of property binding with the parentheses of event binding in a single notation using the ngModel directive. Tip, to remember that the parentheses go inside the brackets, visualize a banana in a box. [()]

In this example, the name property value flows from the component to the input box as with property binding. This is why the input box contains 'World' when the page is displayed. See the application output below.

The user's changes also flow back to the component, updating the name property to the latest value, as with event binding. This is why the heading is updated as the user types a value.

hello-world.component.ts x

The Application

Here is the output from this component.


Two-way Binding Breakdown

Let's break apart the two-way binding syntax to see how it works. The highlighted code shows four different ways to perform two-way binding.

The first two examples use standard Angular property and event binding to manually keep the input fields and component data in sync. They only differ in that one uses the keyup event and the other uses input to trigger the template statement. See the property binding and event binding tutorials for more information about this type of binding syntax.

Next we bind to the ngModel property of the ngModel directive to update the contents of the input field. Conversely, the ngModelChange event takes care of setting of the value of the component property when the user changes the value in the text box.

Now we get to the familiar [(ngModel)] syntax which we saw earlier, and we finish with the canonical form of this statement using bindon-NgModel. Both will do the job so it is just a matter of choice.

two-way-binding.component.ts x

The Application

Here is the output from this component.


Where Next?

More Tutorials

To find out more about Angular components, check out these tutorials.

  • Templates - introduction to inline and external templates.
  • Interpolation - use curly braces and template expressions to output data.
  • Property Binding - update standard properties on the DOM using template expressions.
  • Event Binding - detect standard DOM events and execute template statements.
  • Input Binding - bind to <input> fields such as text, textarea, checkbox, radio and select.
  • Built-in Directives - use ngIf, ngSwitch, ngFor, ngClass and ngStyle to transform the DOM.
  • Component Input Output - use @Input and @Output to pass data in to and out of a component.

Source Code

The source code for this tutorial is available on plunker and in the angular-2-two-way-binding GitHub repo.