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
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
html
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
html
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
html
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
html
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
html
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
FormsModuleforngModel: If[(ngModel)]throws an error, you have not importedFormsModule. 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?
{{ }}? 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.