angular
/

Angular Data Binding – Connecting Logic and UI

Last Sync: Today

On this page

5
0%
5 min read
Remaining
5 minleft

Click any section to jump — progress syncs automatically

angular

Angular Data Binding – Connecting Logic and UI

What is Data Binding?

Data binding is a core feature of Angular that coordinates the communication between a component's TypeScript class (the logic) and its HTML template (the view). As a Technical Lead, you know that efficient data flow is the backbone of a responsive UI. Angular provides four distinct types of binding, categorized by the direction of data flow.

  1. From Source to View (One-Way)

This type of binding pushes data from the TypeScript class into the HTML template. It is used to display dynamic content or set element properties.

  • Interpolation {{ value }}: Used to render text content within HTML tags.
  • Property Binding [property]="value": Used to set the property of an element or a directive (e.g., [disabled], [src], [hidden]).
HTMLRead-only
1

<h1>Welcome, {{ userName }}!</h1>


<img [src]="profileImageUrl">
<button [disabled]="!isFormValid">Submit</button>

  1. From View to Source (One-Way)

This binding handles user interactions, such as clicks, keystrokes, and mouse movements, by calling methods in your TypeScript class.

  • Event Binding (event)="method()": Listens for specific DOM events and triggers logic in the component.
HTMLRead-only
1
<button (click)="onSave()">Save Project</button>
<input (input)="onTyping($event)">

  1. Two-Way Binding

Two-way binding allows for a continuous synchronization of data between the view and the source. If the user changes a value in an input field, the TypeScript variable updates; if the variable is updated via logic, the input field reflects the change.

  • Two-Way Binding [(ngModel)]="value": Commonly used in forms. It combines property binding and event binding syntax (the 'banana in a box').
HTMLRead-only
1
<input [(ngModel)]="projectName">
<p>Editing: {{ projectName }}</p>

Data Binding Summary

TypeSyntaxDirection
Interpolation{{ expression }}Component to View
Property Binding[property]="value"Component to View
Event Binding(event)="handler()"View to Component
Two-Way Binding[(ngModel)]="property"Both Ways

Test Your Knowledge

Q1
of 3

Which syntax is used for property binding in Angular?

A
{{ property }}
B
(property)
C
[property]
D
[(property)]
Q2
of 3

What is the primary use case for Event Binding?

A
Displaying a variable in the UI
B
Handling user interactions like button clicks
C
Passing an object to a child component
D
Formatting a date
Q3
of 3

Which directive is commonly used for Two-Way Data Binding in forms?

A
ngClass
B
ngIf
C
ngModel
D
ngFor

Frequently Asked Questions

What is the 'Banana in a box' syntax?

It refers to the [( )] syntax used for two-way data binding. An easy way to remember the order of brackets is 'the banana (parentheses) goes inside the box (square brackets)'.

Why use [property] instead of {{ }} for attributes?

Interpolation always converts the value to a string. Property binding [ ] allows you to pass non-string values like booleans or objects directly to elements or components.

Does ngModel require a specific module?

Yes. To use [(ngModel)], you must import 'FormsModule' from '@angular/forms' into your Angular Module (or standalone component).

Previous

angular templates

Next

angular directives

Related Content

Need help?

Explore our comprehensive docs or start a chat with our tech experts.