Đây là một ứng dụng Todo đơn giản được xây dựng bằng Angular để học tập. Ứng dụng sử dụng API từ DummyJSON để thực hiện các thao tác CRUD (Create, Read, Update, Delete).
- ✅ Xem danh sách todos với phân trang
- ✅ Thêm todo mới
- ✅ Cập nhật todo (sửa nội dung, đánh dấu hoàn thành)
- ✅ Xóa todo
- ✅ Lấy random todo
- ✅ Lọc todos theo User ID
- ✅ Lọc theo trạng thái (hoàn thành/chưa hoàn thành)
- ✅ Phân trang với limit và skip
- Node.js (v18 hoặc cao hơn)
- npm hoặc yarn
- Cài đặt dependencies:
npm install- Chạy ứng dụng:
npm start- Mở trình duyệt và truy cập:
http://localhost:4200
angular-todo/
├── src/
│ ├── app/
│ │ ├── components/
│ │ │ ├── todo-list/ # Component hiển thị danh sách todos
│ │ │ │ ├── todo-list.component.ts
│ │ │ │ ├── todo-list.component.html
│ │ │ │ └── todo-list.component.css
│ │ │ └── todo-form/ # Component form thêm todo
│ │ │ ├── todo-form.component.ts
│ │ │ ├── todo-form.component.html
│ │ │ └── todo-form.component.css
│ │ ├── models/
│ │ │ └── todo.model.ts # Interfaces và types
│ │ ├── services/
│ │ │ └── todo.service.ts # Service gọi API
│ │ ├── app.component.ts # Root component
│ │ └── app.module.ts # Root module
│ ├── index.html # HTML chính
│ ├── main.ts # Entry point
│ └── styles.css # Global styles
├── angular.json # Angular configuration
├── package.json # Dependencies
└── tsconfig.json # TypeScript configuration
- TodoListComponent: Hiển thị và quản lý danh sách todos
- TodoFormComponent: Form để thêm todo mới
- Mỗi component gồm 3 file: .ts (logic), .html (template), .css (style)
- TodoService: Xử lý tất cả các HTTP requests đến API
- Sử dụng
HttpClientđể gọi API - Injectable để có thể inject vào các component
- Todo: Interface định nghĩa cấu trúc dữ liệu todo
- TodosResponse: Interface cho response từ API
- TodoRequest: Interface cho request gửi đến API
- AppModule: Root module, khai báo tất cả components và imports
- Import
HttpClientModuleđể sử dụng HTTP - Import
FormsModuleđể sử dụng two-way binding (ngModel)
- Property Binding:
[disabled]="!hasPrevPage" - Event Binding:
(click)="deleteTodo(todo.id)" - Two-way Binding:
[(ngModel)]="todoText" - Interpolation:
{{ todo.todo }}
*ngFor: Lặp qua danh sách*ngIf: Hiển thị có điều kiện[class.completed]: Class binding động
constructor(private todoService: TodoService) { }Angular tự động inject TodoService vào component.
this.todoService.getAllTodos().subscribe({
next: (response) => {
/* xử lý dữ liệu */
},
error: (err) => {
/* xử lý lỗi */
},
});- @Output & EventEmitter: TodoFormComponent gửi sự kiện lên parent
@Output() todoAdded = new EventEmitter<{ todo: string, userId: number }>();| Method | Endpoint | Mô tả |
|---|---|---|
| GET | /todos |
Lấy tất cả todos (có phân trang) |
| GET | /todos/:id |
Lấy todo theo ID |
| GET | /todos/random |
Lấy random todo |
| GET | /todos/user/:id |
Lấy todos của user |
| POST | /todos/add |
Thêm todo mới |
| PUT/PATCH | /todos/:id |
Cập nhật todo |
| DELETE | /todos/:id |
Xóa todo |
ngOnInit(): void {
this.loadTodos(); // Chạy khi component được khởi tạo
}get totalPages(): number {
return Math.ceil(this.totalItems / this.itemsPerPage);
}- Tất cả biến và function đều có type
- Interface giúp đảm bảo cấu trúc dữ liệu
error: (err) => {
this.error = 'Không thể tải danh sách todos';
console.error(err);
};- Component quản lý state local (loading, error, todos)
- Service không giữ state, chỉ gọi API
- Global styles trong
styles.css - Component-specific styles trong
.component.css - CSS Grid và Flexbox cho layout responsive
- CSS transitions cho animations
-
API là mock API:
- POST/PUT/DELETE không thực sự thay đổi dữ liệu trên server
- Chỉ simulate response
-
Best Practices:
- Tách logic vào services
- Sử dụng TypeScript types
- Handle errors properly
- Unsubscribe observables khi cần (trong project này Angular tự động unsubscribe)
-
Mở rộng thêm:
- Thêm routing cho nhiều pages
- Sử dụng Reactive Forms thay vì Template-driven Forms
- Thêm state management (NgRx, Akita)
- Thêm animations
- Thêm unit tests
Để xem console logs và network requests:
- Mở DevTools (F12)
- Tab Console: Xem logs và errors
- Tab Network: Xem HTTP requests
- Thêm chức năng search todos
- Thêm loading spinner
- Thêm toast notifications
- Thêm validation cho form
- Thêm sorting (sắp xếp) theo các trường
- Thêm dark mode
- Lưu state vào localStorage
- Thêm animations khi thêm/xóa items
Chúc bạn học tốt! 🚀