0
0
Angularframework~10 mins

Two-way binding with ngModel in Angular - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Two-way binding with ngModel
User types in input field
ngModel updates component variable
Component variable changes
Input field reflects updated variable
Back to User types
This flow shows how user input updates the component variable and how changes in the variable update the input field, creating a loop.
Execution Sample
Angular
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';

@Component({
  selector: 'app-root',
  template: `<input [(ngModel)]="name" /> <p>Hello {{name}}</p>`
})
export class AppComponent {
  name = '';
}
This code binds the input field to the 'name' variable so typing updates 'name' and changes to 'name' update the input.
Execution Table
StepUser InputComponent Variable 'name'ActionRendered Output
1'' (empty)'' (empty)Initial load, variable empty<input value=''> <p>Hello </p>
2'A''' (empty)User types 'A', input changes<input value='A'> <p>Hello </p>
3'A''A'ngModel updates 'name' to 'A'<input value='A'> <p>Hello A</p>
4'Al''A'User types 'l', input changes<input value='Al'> <p>Hello A</p>
5'Al''Al'ngModel updates 'name' to 'Al'<input value='Al'> <p>Hello Al</p>
6'Ali''Al'User types 'i', input changes<input value='Ali'> <p>Hello Al</p>
7'Ali''Ali'ngModel updates 'name' to 'Ali'<input value='Ali'> <p>Hello Ali</p>
8User clears input'Ali'Input cleared, input changes<input value=''> <p>Hello Ali</p>
9'' (empty)'' (empty)ngModel updates 'name' to empty<input value=''> <p>Hello </p>
10No more input'' (empty)No change, stable state<input value=''> <p>Hello </p>
💡 User stops typing, variable and input are synchronized and stable.
Variable Tracker
VariableStartAfter Step 3After Step 5After Step 7After Step 9
name'' (empty)'A''Al''Ali''' (empty)
Key Moments - 3 Insights
Why does the displayed text update only after ngModel updates the variable?
Because the input change triggers ngModel to update the component variable first (see steps 2 and 3), then Angular updates the displayed text using the new variable value.
What happens if the component variable changes programmatically?
The input field updates to reflect the new variable value automatically, completing the two-way binding loop (not shown in this user input trace but implied by the flow).
Why is the input value and variable sometimes temporarily out of sync?
Because user input changes the input field first, then ngModel updates the variable on the next step (see steps 2 and 3), causing a brief difference.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'name' after step 5?
A'Al'
B'A'
C'Ali'
D'' (empty)
💡 Hint
Check the 'Component Variable' column at step 5 in the execution_table.
At which step does the input field first show 'Ali'?
AStep 5
BStep 7
CStep 6
DStep 4
💡 Hint
Look at the 'User Input' column and 'Rendered Output' column in the execution_table.
If the user clears the input at step 8, what happens to the variable 'name' next?
AIt stays 'Ali'
BIt updates to empty string
CIt becomes null
DIt throws an error
💡 Hint
See step 9 in the execution_table where ngModel updates the variable after input clears.
Concept Snapshot
Two-way binding with ngModel syntax: <input [(ngModel)]="variable" />
User input updates the variable, variable changes update input.
Angular syncs input and variable automatically.
Useful for forms and interactive UI.
Requires importing FormsModule.
Creates a loop: input <-> variable <-> input.
Full Transcript
Two-way binding with ngModel in Angular connects an input field and a component variable so they update each other automatically. When the user types in the input, ngModel updates the variable. When the variable changes, the input field updates to match. This creates a loop that keeps UI and data in sync. The example code shows an input bound to a variable 'name'. As the user types letters, the variable updates step-by-step, and the displayed greeting updates accordingly. The execution table traces each step of user input, variable changes, and rendered output. Key moments clarify why input and variable can be briefly out of sync and how changes flow both ways. The visual quiz tests understanding of variable values and input display at different steps. This binding requires importing Angular's FormsModule and is essential for building interactive forms and UI elements.