Complete todo list CRUD project in angular 18 with example
Creating a simple CRUD (Create, Read, Update, Delete) Todo List application in Angular 18 involves setting up Angular components, services, and using forms for input. We'll assume the Angular CLI is already installed and set up.
Step-by-Step Guide:
- Set Up Angular Project
- Create Todo Service
- Create Components: Todo List, Todo Form, Todo Item
- Wire Everything Together
- Add Routing for Navigation (Optional)
1. Set Up Angular Project
Create a new Angular project using the Angular CLI:
This will start a development server at http://localhost:4200
.
2. Install Angular Forms Module
To use Angular forms (template-driven or reactive forms), you need to import FormsModule
and ReactiveFormsModule
into your app.
Open app.module.ts
and import these modules:
3. Create Todo Service
The TodoService will handle CRUD operations like creating, retrieving, updating, and deleting tasks.
Create todo.service.ts
inside the src/app/
folder:
This service uses BehaviorSubject
to hold and update the list of todos.
4. Create Todo Components
Todo List Component (todo-list.component.ts
)
The TodoListComponent displays the list of tasks.
todo-list.component.html
Add some styles for the completed tasks (optional):
Todo Form Component (todo-form.component.ts
)
This component is used to add a new Todo.
todo-form.component.html
5. Wire Everything Together
Now, in the main app.component.html
, use the TodoListComponent and TodoFormComponent:
6. Optionally Add Routing (for better organization)
You can add routing in your application to navigate between different pages like "All Todos", "Completed Todos", etc.
Add Routes in app-routing.module.ts
:
Import AppRoutingModule
in app.module.ts
:
And update your app.component.html
to add a router outlet:
7. Running the Application
To start your application, use the following command:
Navigate to http://localhost:4200
to view your CRUD Todo list application.
Conclusion:
This is a basic Todo List CRUD app using Angular 18 with services, components, and form handling. You can enhance it by adding features like editing todos, saving data in local storage, or adding animations for task updates.
Comments
Post a Comment