Angular - Component trong Angular
Component gồm lớp TypeScript, template và style. Bài này tạo component bằng CLI, truyền dữ liệu bằng input(), phát sự kiện bằng output() và lồng component.
Bạn sẽ học được
- Tạo component bằng ng generate
- Hiểu decorator @Component
- Truyền dữ liệu bằng input()
- Phát sự kiện bằng output()
Cơ sở lý thuyết
Component trong Angular là một lớp TypeScript được đánh dấu bằng decorator @Component, gồm ba phần: lớp (dữ liệu và hành vi), template (giao diện) và style (kiểu). Mỗi component có một selector – thẻ HTML tùy biến để dùng nó, ví dụ <app-the-sinh-vien>.
- Standalone component (mặc định từ Angular 19) tự khai báo các phụ thuộc trong imports, không cần NgModule.
- Đóng gói kiểu (view encapsulation): CSS của component mặc định chỉ áp dụng trong component đó (Angular giả lập bằng thuộc tính _ngcontent).
- Vòng đời: constructor → ngOnInit (khởi tạo, nơi tải dữ liệu) → ngOnChanges (khi input đổi) → ngOnDestroy (dọn dẹp hủy đăng ký, hẹn giờ).
- input() và output() (API dạng signal) để nhận dữ liệu từ cha và phát sự kiện lên cha.
- Component thông minh và component trình bày (smart/presentational): component trang lo tải dữ liệu, component con chỉ nhận input và phát output – dễ tái sử dụng và kiểm thử.
Tạo component
ng generate component the-sinh-vien
# viết tắt: ng g c the-sinh-vienInput và output
// the-sinh-vien.ts
import { Component, input, output } from "@angular/core";
@Component({
selector: "app-the-sinh-vien",
template: `
<div class="the">{{ sv().ten }} - {{ sv().diem }}
<button (click)="xoa.emit(sv().ma)">Xóa</button>
</div>`,
styles: ".the { padding: 8px; border: 1px solid #ddd; }"
})
export class TheSinhVien {
sv = input.required<{ ma: string; ten: string; diem: number }>();
xoa = output<string>();
}// app.ts: imports: [TheSinhVien]
<app-the-sinh-vien [sv]="s" (xoa)="xoaSV($event)" />Ghi chú
selectorlà tên thẻ dùng trong template khác.- Component standalone khai báo các component con trong mảng
imports. - Bản cũ dùng
@Input(),@Output()và EventEmitter; cách mới input(), output() trả về signal.
Ứng dụng thực tế
Bài toán: component thẻ sinh viên dùng lại ở nhiều trang: nhận dữ liệu qua input bắt buộc, phát sự kiện khi bấm "Xem điểm", tự tô viền theo học lực:
ng generate component the-sinh-vien@Component({
selector: "app-the-sinh-vien",
template: `
<div class="the" [class.canh-bao]="sv().diemTB < 2">
<b>{{ sv().ten }}</b> – {{ sv().lop }} – GPA {{ sv().diemTB }}
<button (click)="xemDiem.emit(sv().ma)">Xem điểm</button>
</div>`,
styles: [".the{border:1px solid #ccc;padding:8px} .canh-bao{border-color:red}"],
})
export class TheSinhVienComponent {
sv = input.required<SinhVien>();
xemDiem = output<string>();
}
// Trang cha
// <app-the-sinh-vien [sv]="s" (xemDiem)="moBangDiem($event)" />Thực hành
- Tạo component TheSinhVien nhận dữ liệu qua input.
- Phát sự kiện xóa lên component cha.
- Hiển thị 3 thẻ sinh viên trong App.
Tóm tắt bài học
- Component = lớp + template + style.
- input() nhận dữ liệu, output() phát sự kiện.
- Khai báo component con trong imports.
Các bài khác …
Trang 4 / 11

