Explain it simply
Two-Way Binding [( )], in plain language
Two-Way Data Binding [(...)] (the "Banana in a Box" syntax) combines property binding and event binding into one. It's most commonly used with form inputs via the [(ngModel)] directive.
Problem → Why → Solution
The problem — why this concept matters
Allowing both parent and child to silently mutate one object obscures the source of truth.
A common mistake
One happy-path result does not prove the feature works; empty, loading, error, and cleanup behavior need explicit tests.
Core concept — understand how it works
Two-way binding combines a property input and matching change output through the [()] syntax. In forms, [(ngModel)] synchronizes the view and component state, but unclear ownership can turn it into hidden mutation. Understand the value/valueChange contract for reusable components and consider reactive forms for complex workflows.
- input and state
- component and template
- rendered Angular UI
Explain the core concept and Angular data flow of Two-Way Binding [( )]
Core syntax or pattern
// app.module.ts
import { FormsModule } from '@angular/forms';
@NgModule({
imports: [BrowserModule, FormsModule]
})
// component.html
<input [(ngModel)]="username" type="text">
<p>Hello, {{ username }}!</p>
// component.ts
export class MyComponent {
username = 'World';
}Two-way binding combines a property input and matching change output through the [()] syntax. In forms, [(ngModel)] synchronizes the view and component state, but unclear ownership can turn it into hidden mutation. Understand the value/valueChange contract for reusable components and consider reactive forms for complex workflows.
Read the code step by step
- input and state
// app.module.tsTwo-Way Data Binding [(...)] (the "Banana in a Box" syntax) combines property binding and event binding into one. It's most commonly used with form inputs via the [(ngModel)] directive.
- component and template
<input [(ngModel)]="username" type="text">Two-way binding combines a property input and matching change output through the [()] syntax. In forms, [(ngModel)] synchronizes the view and component state, but unclear ownership can turn it into hidden mutation. Understand the value/valueChange contract for reusable components and consider reactive forms for complex workflows.
- rendered Angular UI
}Bind a search field with ngModel, test trimmed queries and clearing, then model the same contract as a custom input/output pair.
- input and state
- component and template
- rendered Angular UI
Success
Explain the core concept and Angular data flow of Two-Way Binding [( )]
Failure
Allowing both parent and child to silently mutate one object obscures the source of truth.
Two-Way Data Binding [(...)] (the "Banana in a Box" syntax) combines property binding and event binding into one. It's most commonly used with form inputs via the [(ngModel)] directive.
⚠️ Important: FormsModule needs to be imported.
// app.module.ts
import { FormsModule } from '@angular/forms';
@NgModule({
imports: [BrowserModule, FormsModule]
})
// component.html
<input [(ngModel)]="username" type="text">
<p>Hello, {{ username }}!</p>
// component.ts
export class MyComponent {
username = 'World';
}(As soon as you type into the input box, the text below instantly updates to 'Hello, [typed text]!')Real-world usage
How to apply it in a real project
Bind a search field with ngModel, test trimmed queries and clearing, then model the same contract as a custom input/output pair.
Mini challenge
Build it yourself
Rebuild the Two-Way Binding [( )] example without copying it. Add two normal states, one edge case, and one failure state, and write the expected UI and state transition before running it.
- Rebuild the example without copying it
- Test a normal input and an edge case
- Predict the compiler or output result before running it
// app.module.ts
import { FormsModule } from '@angular/forms';
@NgModule({
imports: [BrowserModule, FormsModule]
})
// component.html
<input [(ngModel)]="username" type="text">
<p>Hello, {{ username }}!</p>
// component.ts
export class MyComponent {
username = 'World';
}Expected: Explain the core concept and Angular data flow of Two-Way Binding [( )]
Show hint
Bind a search field with ngModel, test trimmed queries and clearing, then model the same contract as a custom input/output pair.
Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- input and state → component and template → rendered Angular UI
- Two-way binding combines a property input and matching change output through the [()] syntax. In forms, [(ngModel)] synchronizes the view and component state, but unclear ownership can turn it into hidden mutation. Understand the value/valueChange contract for reusable components and consider reactive forms for complex workflows.
- Allowing both parent and child to silently mutate one object obscures the source of truth.
- Bind a search field with ngModel, test trimmed queries and clearing, then model the same contract as a custom input/output pair.
Angular — Templates — Angular