Angular CRUD Integration
Learn how Angular applications implement Create, Read, Update and Delete operations by communicating with backend REST APIs.
What is CRUD?
CRUD stands for Create, Read, Update and Delete.
These four operations represent the basic actions commonly performed on application data.
In Angular, CRUD operations are commonly implemented by using HttpClient to communicate with backend REST APIs.
CRUD Flow
Four CRUD Operations
Create
Add a new record through the backend API.
Read
Retrieve existing records from the backend.
Update
Modify an existing record.
Delete
Remove an existing record.
CRUD to HTTP Method Mapping
| CRUD Operation | HTTP Method | HttpClient | Purpose |
|---|---|---|---|
| Create | POST |
http.post()
|
Add new resource |
| Read | GET |
http.get()
|
Retrieve resource |
| Update | PUT |
http.put()
|
Update resource |
| Delete | DELETE |
http.delete()
|
Remove resource |
Step 1: Create a Data Model
A TypeScript interface can describe the structure of the resource used by the CRUD operations.
export interface Course {
id: number;
name: string;
duration: string;
fees: number;
}
Step 2: Create CRUD Service
import {
Injectable
} from '@angular/core';
import {
HttpClient
} from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class CourseService {
private apiUrl =
'/api/courses';
constructor(
private http: HttpClient
) {
}
}
1. Create Operation
The Create operation adds a new record to the backend.
Angular commonly sends the new resource using an HTTP POST request.
addCourse(course: Course) {
return this.http.post<Course>(
this.apiUrl,
course
);
}
course: Course = {
id: 0,
name: 'Java Full Stack',
duration: '6 Months',
fees: 35000
};
addCourse() {
this.courseService
.addCourse(this.course)
.subscribe({
next: response => {
console.log(
'Course created:',
response
);
},
error: error => {
console.error(error);
}
});
}
Create Operation Flow
2. Read Operation
The Read operation retrieves existing records from the backend.
Angular commonly uses an HTTP GET request for this operation.
getCourses() {
return this.http.get<Course[]>(
this.apiUrl
);
}
courses: Course[] = [];
loadCourses() {
this.courseService
.getCourses()
.subscribe({
next: courses => {
this.courses = courses;
},
error: error => {
console.error(error);
}
});
}
Courses Loaded
Java Full Stack
.NET Full Stack
Python Full Stack
Read a Single Record
getCourse(id: number) {
return this.http.get<Course>(
this.apiUrl + '/' + id
);
}
loadCourse(id: number) {
this.courseService
.getCourse(id)
.subscribe({
next: course => {
console.log(course);
},
error: error => {
console.error(error);
}
});
}
3. Update Operation
The Update operation modifies an existing backend resource.
Angular can send the updated resource using an HTTP PUT request.
updateCourse(
id: number,
course: Course
) {
return this.http.put<Course>(
this.apiUrl + '/' + id,
course
);
}
updateCourse(course: Course) {
this.courseService
.updateCourse(
course.id,
course
)
.subscribe({
next: updatedCourse => {
console.log(
'Updated:',
updatedCourse
);
},
error: error => {
console.error(error);
}
});
}
Update Operation Flow
4. Delete Operation
The Delete operation removes an existing backend resource.
Angular can send the resource identifier through an HTTP DELETE request.
deleteCourse(id: number) {
return this.http.delete(
this.apiUrl + '/' + id
);
}
deleteCourse(id: number) {
this.courseService
.deleteCourse(id)
.subscribe({
next: () => {
console.log(
'Course deleted'
);
},
error: error => {
console.error(error);
}
});
}
Delete Operation Flow
Complete CRUD Service
import {
Injectable
} from '@angular/core';
import {
HttpClient
} from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class CourseService {
private apiUrl =
'/api/courses';
constructor(
private http: HttpClient
) {
}
// CREATE
addCourse(course: Course) {
return this.http.post<Course>(
this.apiUrl,
course
);
}
// READ
getCourses() {
return this.http.get<Course[]>(
this.apiUrl
);
}
// READ SINGLE
getCourse(id: number) {
return this.http.get<Course>(
this.apiUrl + '/' + id
);
}
// UPDATE
updateCourse(
id: number,
course: Course
) {
return this.http.put<Course>(
this.apiUrl + '/' + id,
course
);
}
// DELETE
deleteCourse(id: number) {
return this.http.delete(
this.apiUrl + '/' + id
);
}
}
Complete CRUD Architecture
CRUD Component Example
export class CoursesComponent {
courses: Course[] = [];
loading = false;
errorMessage = '';
loadCourses() {
this.loading = true;
this.errorMessage = '';
this.courseService
.getCourses()
.subscribe({
next: courses => {
this.courses = courses;
this.loading = false;
},
error: error => {
console.error(error);
this.errorMessage =
'Unable to load courses.';
this.loading = false;
}
});
}
addCourse(course: Course) {
this.courseService
.addCourse(course)
.subscribe({
next: createdCourse => {
this.courses = [
...this.courses,
createdCourse
];
},
error: error => {
console.error(error);
}
});
}
updateCourse(course: Course) {
this.courseService
.updateCourse(
course.id,
course
)
.subscribe({
next: updatedCourse => {
this.courses =
this.courses.map(
item =>
item.id ===
updatedCourse.id
? updatedCourse
: item
);
},
error: error => {
console.error(error);
}
});
}
deleteCourse(id: number) {
this.courseService
.deleteCourse(id)
.subscribe({
next: () => {
this.courses =
this.courses.filter(
course =>
course.id !== id
);
},
error: error => {
console.error(error);
}
});
}
}
CRUD Template Example
<button
class="btn btn-primary"
(click)="loadCourses()">
Load Courses
</button>
@if (loading) {
<p>
Loading...
</p>
}
@if (errorMessage) {
<div class="alert alert-danger">
{{ errorMessage }}
</div>
}
@for (course of courses; track course.id) {
<div class="course-card">
<h3>
{{ course.name }}
</h3>
<p>
Duration:
{{ course.duration }}
</p>
<p>
Fees:
₹{{ course.fees }}
</p>
<button
class="btn btn-outline-primary"
(click)="updateCourse(course)">
Edit
</button>
<button
class="btn btn-outline-danger"
(click)="deleteCourse(course.id)">
Delete
</button>
</div>
}
CIIT Training Institute CRUD Example
Suppose the CIIT Training Institute has an admin dashboard where authorized staff can manage training course information.
The Angular frontend can use CRUD operations to create new courses, load existing courses, update course details and delete records through the backend API.
const newCourse: Course = {
id: 0,
name: '.NET Full Stack',
duration: '6 Months',
fees: 35000
};
this.courseService
.addCourse(newCourse)
.subscribe({
next: course => {
console.log(
'Created:',
course
);
}
});
this.courseService
.getCourses()
.subscribe({
next: courses => {
this.courses = courses;
}
});
const updatedCourse:
Course = {
id: 101,
name: 'Java Full Stack',
duration: '8 Months',
fees: 40000
};
this.courseService
.updateCourse(
updatedCourse.id,
updatedCourse
)
.subscribe({
next: course => {
console.log(
'Updated:',
course
);
}
});
this.courseService
.deleteCourse(101)
.subscribe({
next: () => {
console.log(
'Course deleted'
);
}
});
CIIT Training Institute Admin
Create: Add new training course
Read: View available courses
Update: Modify course details
Delete: Remove a course record
CRUD Operation States
Loading
Indicates that an API operation is currently running.
Success
The requested CRUD operation completed successfully.
Error
The application handles a failed API operation.
Refresh
The UI can reflect the latest backend data after changes.
Complete CRUD Reference
| Operation | Method | Example Endpoint | Angular Method |
|---|---|---|---|
| Create | POST | /api/courses |
http.post()
|
| Read All | GET | /api/courses |
http.get()
|
| Read One | GET | /api/courses/101 |
http.get()
|
| Update | PUT | /api/courses/101 |
http.put()
|
| Delete | DELETE | /api/courses/101 |
http.delete()
|
CRUD Integration Best Practices
- Keep CRUD API calls inside dedicated Angular services.
- Use TypeScript interfaces or types for request and response data.
- Handle loading, success and error states appropriately.
- Update the UI after successful create, update or delete operations.
- Keep API endpoints centralized instead of duplicating URLs.
- Validate user input before sending create or update requests.
Important Points
POST is commonly used to send a new resource to the API.
GET retrieves existing resources from the backend.
PUT can send updated information for an existing resource.
DELETE requests remove a resource identified by its API endpoint.
Summary
CRUD represents Create, Read, Update and Delete operations commonly required for managing application data.
Angular can implement CRUD through a service using HttpClient and the HTTP methods POST, GET, PUT and DELETE.
A typical flow is Angular UI → Component → Service → HttpClient → REST API → Database . In the next topic, we will learn how to handle API Responses properly.