Skip to main content
Angular Basics
CHAPTER 06 Beginner

Templates and Data Binding

Updated: May 18, 2026
5 min read

# CHAPTER 6

Templates and Data Binding

1. Chapter Introduction

Data Binding is how Angular keeps your TypeScript class and the HTML template in sync. Without data binding, you would have to manually select DOM elements and update them whenever data changes. Angular eliminates this completely. When the TypeScript data changes, the HTML updates automatically. When the user clicks a button, the TypeScript logic fires automatically. This is the magic of data binding.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Use Interpolation ({{ }}) to display class properties.
  • Use Property Binding ([ ]) to set HTML attributes dynamically.
  • Use Event Binding (( )) to react to user interactions.
  • Use Two-Way Binding ([( )]) to synchronize form inputs.
  • Build a live form preview mini project.

3. The Four Types of Data Binding

text
123
TypeScript Class -----> HTML Template   (One-Way: Interpolation, Property Binding)
TypeScript Class <----- HTML Template   (One-Way: Event Binding)
TypeScript Class <----> HTML Template   (Two-Way: ngModel)

4. Interpolation {{ }}

Interpolation reads a property from your TypeScript class and renders it as text in the template. It is read-only: the HTML template cannot modify the data this way.
greeting.component.ts
12345
export class GreetingComponent {
  title: string = &#039;Welcome to Angular!&#039;;
  currentYear: number = 2024;
  username: string = &#039;Alice&#039;;
}
html
1234567
<!-- greeting.component.html -->
<h1>{{ title }}</h1>
<p>Hello, {{ username }}! Copyright {{ currentYear }}</p>

<!-- You can also use expressions! -->
<p>Next year: {{ currentYear + 1 }}</p>
<p>Uppercase: {{ username.toUpperCase() }}</p>

5. Property Binding [ ]

Property Binding sets the value of an HTML attribute dynamically. Instead of a static attribute like src="...", you wrap it in square brackets to connect it to a TypeScript property.
typescript
1234
export class ImageComponent {
  imageUrl: string = &#039;https://example.com/avatar.png';
  buttonDisabled: boolean = true;
}
html
12345
<!-- Sets the &#039;src' attribute to the imageUrl property value -->
<img [src]="imageUrl" alt="User Avatar" />

<!-- Disables the button if &#039;buttonDisabled' is true -->
<button [disabled]="buttonDisabled">Submit</button>

6. Event Binding ( )

Event Binding listens for user actions (like clicks, key presses) and calls a method in your TypeScript class. You wrap the event name in parentheses.
typescript
1234567891011
export class CounterComponent {
  count: number = 0;

  increment(): void {
    this.count++;
  }

  reset(): void {
    this.count = 0;
  }
}
html
12345
<h2>Count: {{ count }}</h2>

<!-- Calls increment() when clicked -->
<button (click)="increment()">+1</button>
<button (click)="reset()">Reset</button>

7. Two-Way Binding [( )] (Banana in a Box)

Two-Way Binding combines property binding and event binding. When the user types in an input field, the TypeScript property updates instantly. When the TypeScript property changes, the input field reflects it instantly. Developers affectionately call [( )] the "banana in a box" syntax!

*Note: For Two-Way Binding with ngModel, you must import FormsModule in your module or component.*

typescript
1234
export class LivePreviewComponent {
  username: string = &#039;&#039;;
  email: string = &#039;&#039;;
}
html
12345678
<input [(ngModel)]="username" placeholder="Enter your name" />
<input [(ngModel)]="email" placeholder="Enter your email" />

<!-- Live preview updates as you type! -->
<div class="preview">
  <p>Name: {{ username }}</p>
  <p>Email: {{ email }}</p>
</div>

8. Mini Project: Live Form Preview App

As the user types in the input fields, the preview card below updates in real-time!
profile-editor.component.ts
1234567891011121314
import { Component } from &#039;@angular/core&#039;;
import { FormsModule } from &#039;@angular/forms&#039;;

@Component({
  selector: &#039;app-profile-editor&#039;,
  standalone: true,
  imports: [FormsModule],
  templateUrl: &#039;./profile-editor.component.html&#039;
})
export class ProfileEditorComponent {
  name: string = &#039;Your Name&#039;;
  role: string = &#039;Your Role&#039;;
  bio: string = &#039;Your bio here...&#039;;
}
html
12345678910111213141516
<!-- profile-editor.component.html -->
<div class="editor-layout">
  <div class="form-panel">
    <h3>Edit Profile</h3>
    <input [(ngModel)]="name" placeholder="Name" />
    <input [(ngModel)]="role" placeholder="Role" />
    <textarea [(ngModel)]="bio" placeholder="Bio"></textarea>
  </div>

  <div class="preview-panel">
    <h3>Live Preview</h3>
    <h2>{{ name }}</h2>
    <span>{{ role }}</span>
    <p>{{ bio }}</p>
  </div>
</div>

9. Common Mistakes

  • Using {{ }} for HTML attributes: <img src="{{ imageUrl }}" /> works but is considered bad practice for non-string attributes. Always use [src]="imageUrl" for attributes.
  • Forgetting FormsModule for ngModel: If [(ngModel)] throws an error, you have not imported FormsModule. Add it to the imports array.

10. MCQs with Answers

Question 1

Which syntax is used for Interpolation to display a TypeScript property in an HTML template?

Question 2

How would you dynamically set a button's disabled attribute to a boolean variable isDisabled?

Question 3

What type of binding is (click)="doSomething()"?

Question 4

What is the nickname for the Two-Way Binding syntax [( )]?

Question 5

Which Angular module must be imported to use [(ngModel)] for two-way data binding?

Question 6

If you type in an input bound with [(ngModel)]="username", what happens to username in the TypeScript class?

Q7. Can you use expressions inside {{ }}? For example, {{ 5 + 5 }}? a) Yes b) No Answer: a) Yes.
Question 8

What is the direction of data flow in Property Binding?

Question 9

Which of the following correctly listens for a keyup event on an input?

Question 10

What is the key benefit of Two-Way Data Binding for forms?

11. Interview Questions

  • Q: Explain the difference between Property Binding and Interpolation. When would you use one over the other?
  • Q: How does two-way data binding with [(ngModel)] work internally? (Answer: It is syntactic sugar for [ngModel]="val" (ngModelChange)="val=$event")

12. Summary

Data Binding is the heartbeat of Angular. Interpolation displays data, Property Binding sets attributes, Event Binding reacts to the user, and Two-Way Binding creates a live, synchronized connection between the UI and the data layer. Mastering these four patterns gives you the power to build any interactive interface.

13. Next Chapter Recommendation

Data Binding lets us display and react to data. But what if we want to show or hide an element based on a condition, or repeat a component for every item in a list? In Chapter 7: Directives in Angular, we will learn how to manipulate the DOM structure dynamically using *ngIf and *ngFor.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·