Two Way Data Binding in Angular - ngModel vs Signals
Two-Way Data Binding in Angular
Two-way data binding means data stays synced between the TypeScript file and the HTML file.
If data changes in HTML, it updates in TypeScript.
If data changes in TypeScript, it updates in HTML.
Two-Way Data Binding with Signals
A signal is created in the TypeScript file.
The signal data is displayed in HTML using interpolation.
Updating Signal Data from Input
Input fields are used to update signal values.
The input event sends the updated value to a function.
Updating Object Inside Signal
A single function is used to update any property of the object.
The update() method creates a new object and updates only the required property using a dynamic key.
Updating Multiple Fields Using Same Function
The same function updates name, age, and email.
Each input updates only its related field.
Result
When the user types in any input:
- The signal updates
- The UI updates automatically
- Data stays synced between HTML and TypeScript
This is two-way data binding using signals and objects.
Interview Explanation (Simple)
Two-way data binding means when data changes in HTML, it updates in TypeScript, and when data changes in TypeScript, it updates in HTML automatically.