Data Binding
Data binding is a key feature of Angular that keeps your apps data and user interface (UI) in sync.
Simply put
Simply put
- The model is your apps data (like text, numbers, lists).
- The view is what the user sees on the screen (HTML).
Example: Data Binding in Action
app.component.ts (TypeScript file with data)
import { Component } from '@angular/core';
@Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css']})export class AppComponent { title = 'Quipoin!!'; days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];}
app.component.html (HTML template)
<div style="text-align: center;"> <h1> Welcome to {{title}} </h1></div>
<div>Days: <select> <option *ngFor="let i of days">{{i}}</option> </select></div>
How It Works (Step by Step)
{{title}} - This is called interpolation.
*ngFor="let i of days" - This is called a structural directive.
{{title}} - This is called interpolation.
- It displays the value of the title property from the component.
- If you change title in the TypeScript file, the heading will update automatically in the browser.
*ngFor="let i of days" - This is called a structural directive.
- It loops over each item in the days array and creates an <option> for each day.
- If you add or remove days in the array, the dropdown updates automatically.
Why Data Binding Matters
- Keeps your app dynamic and responsive
- Saves time: no need to manually update the UI when data changes
- Makes your code cleaner and easier to maintain