🔗
Basics

Data Binding

Property, Event, Two-Way
💡 Data binding ek do-tarfa (ya ek-tarfa) telephone line hai — Property binding ek-tarfa hai (component se template ko batana), Event binding ulta hai (template se component ko batana "kuch hua"), Two-way binding dono directions ek saath (jaise WhatsApp call, dono bol sakte hain).

Property binding [property]="value" — component data ko HTML element property se JODता hai (jaise [disabled]="isLoading"). Event binding (event)="handler()" — user interactions (click, input) ko component method se JODता hai. Two-way binding [(ngModel)]="value" — dono ek saath, form inputs ke liye bahut common.

// Property binding — component → template:
// <img [src]="imageUrl">
// <button [disabled]="isLoading">Submit</button>

// Event binding — template → component:
// <button (click)="onSubmit()">Submit</button>
// <input (keyup)="onKeyUp($event)">

// Two-way binding — dono directions:
// <input [(ngModel)]="userName">
// Jab user type kare, userName update hota hai
// Jab userName (code se) change ho, input bhi update hota hai
🔗
Data binding ek do-tarfa (ya ek-tarfa) telephone line hai — Property binding ek-tarfa hai (component se template ko batana), Event binding ulta hai (template se component ko batana "kuch hua"), Two-way binding dono directions ek saath (jaise WhatsApp call, dono bol sakte hain).
1 / 6
⚡ झट से Recap
  • [property]="value" = property binding (component → template)
  • (event)="handler()" = event binding (template → component)
  • [(ngModel)]="value" = two-way binding, forms ke liye common
इस page में (2 subtopics)

Property binding ([property]) DOM PROPERTY set karta hai (JavaScript object property). Attribute binding ([attr.name]) HTML ATTRIBUTE set karta hai — zyadatar cases mein property binding kaafi hai, lekin kuch HTML attributes (jaise colspan, aria-*) ka koi corresponding DOM property nahi hota, unke liye attr. zaroori hai.

// Property binding (zyadatar cases):
// <img [src]="imageUrl">

// Attribute binding (jab koi DOM property nahi hoti):
// <td [attr.colspan]="columnCount"></td>
// <button [attr.aria-label]="buttonLabel"></button>
💡Tip: Rule of thumb: pehle property binding try karo ([property]) — sirf tab attr. use karo jab Angular warning de ki "no corresponding property" ho.

[class.name] conditionally ek CSS class toggle karta hai. [style.property] directly ek CSS style set karta hai — dynamic styling ke liye common, especially conditional states (active, error, disabled) ke liye.

// Conditional class:
// <div [class.active]="isActive">Content</div>
// isActive true ho to "active" class add hoti hai

// Multiple classes (object syntax):
// <div [ngClass]="{ 'active': isActive, 'error': hasError }">

// Direct style:
// <div [style.color]="isError ? 'red' : 'black'">Text</div>
💡Tip: [ngClass] multiple conditional classes ke liye zyada scalable hai single [class.name] bindings se — object syntax se ek saath kai classes conditionally control kar sakte ho.