Component Binding + Signals

Angular-এ component binding আর signals — দুটোই খুব গুরুত্বপূর্ণ concept, especially modern Angular (v16+) এ। সহজভাবে বুঝাই 👇


🔹 1. Component Binding কী?

👉 Component binding মানে HTML (template) ↔ TypeScript (component) এর মধ্যে data pass করা।

Angular-এ mainly 4 ধরনের binding আছে:


🔸 (1) Interpolation {{ }}

👉 TS → HTML data show করা

<h1>{{ title }}</h1>
title = "My App";

🔸 (2) Property Binding [ ]

👉 HTML property set করা

<img [src]="imageUrl">

🔸 (3) Event Binding ( )

👉 user action handle করা

<button (click)="onClick()">Click</button>

🔸 (4) Two-way Binding [( )]

👉 দুই দিকেই data flow

<input [(ngModel)]="name">

👉 এর জন্য FormsModule লাগে


🔥 Summary (Binding)

  • Data flow between TS ↔ HTML
  • UI update করার core mechanism

🔹 2. Signals কী? (Modern Angular 🔥)

👉 Signals = reactive state management system

Angular-এ আগে change detection একটু heavy ছিল
Signals এটা optimize করে


🔸 Signal create করা

import { signal } from '@angular/core';

count = signal(0);

🔸 Value access করা

count();   // get value

🔸 Update করা

count.set(5);
count.update(v => v + 1);

🔸 Template এ use

<p>{{ count() }}</p>

🔥 Example

count = signal(0);

increment() {
  this.count.update(v => v + 1);
}
<button (click)="increment()">+</button>
<p>{{ count() }}</p>

👉 button চাপলে UI automatically update হবে ⚡


🔥 Signals vs Traditional Approach

FeatureOld (Observable/Zone)Signals
Complexityবেশিকম
Performanceকম efficientবেশি fast ⚡
Reactivityindirectdirect

🔹 Signals related আরও কিছু

  • computed() → derived value
  • effect() → side effect run
double = computed(() => this.count() * 2);

🔥rning এর জন্য খুব useful হবে 👍

চল একটা real Angular mini example করি — যেখানে
👉 API call + Component Binding + Signals একসাথে use হবে 🔥


🎯 Scenario

আমরা একটা Product List দেখাবো API থেকে


🔹 Step 1: environment setup (API URL)

// environments/environment.ts
export const environment = {
  apiUrl: 'https://dummyjson.com'
};

🔹 Step 2: Service তৈরি (API call)

// product.service.ts
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { environment } from '../environments/environment';

@Injectable({
  providedIn: 'root'
})
export class ProductService {

  constructor(private http: HttpClient) {}

  getProducts() {
    return this.http.get<any>(`${environment.apiUrl}/products`);
  }
}

🔹 Step 3: Component (Signals use করে)

// product.component.ts
import { Component, signal } from '@angular/core';
import { ProductService } from './product.service';

@Component({
  selector: 'app-product',
  templateUrl: './product.component.html'
})
export class ProductComponent {

  products = signal<any[]>([]);
  loading = signal(true);

  constructor(private productService: ProductService) {}

  ngOnInit() {
    this.productService.getProducts().subscribe(res => {
      this.products.set(res.products);
      this.loading.set(false);
    });
  }
}

🔹 Step 4: Template (Binding use করে)

<!-- product.component.html -->

<h2>Product List</h2>

<p *ngIf="loading()">Loading...</p>

<div *ngFor="let p of products()">
  <h3>{{ p.title }}</h3>
  <p>{{ p.price }}$</p>
</div>

🔥 এখানে কী কী use হলো?

✅ 1. Component Binding

  • {{ p.title }} → Interpolation
  • *ngFor → data loop
  • *ngIf → condition

✅ 2. Signals

  • products = signal([]) → state
  • products.set() → update
  • products() → read

👉 UI auto update হচ্ছে ⚡


✅ 3. API Call

  • HttpClient দিয়ে data আনছে
  • Service clean architecture maintain করছে

🔥 Flow টা বুঝো

  1. Component load হয়
  2. API call করে
  3. data আসলে → signal update
  4. signal change → UI auto update

🔥 Bonus (Upgrade Idea 🔥)

তুমি চাইলে এটা আরও advance করতে পারো:

👉 Loading spinner

👉 Error handling

👉 Search filter (signal দিয়ে)


🔥 Real বুঝার key point

👉 Angular app এ:

  • Binding = data show
  • Service = API call
  • Signal = state control + auto UI update

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top