W05 - Consume CRUD Operations With Angular 15
W05 - Consume CRUD Operations With Angular 15
In this workshop, we will explore how to consume CRUD operations with Angular
15. This approach will enable you to create a dynamic application with
components tailored to each functionality. Follow these steps to successfully
integrate these features.
BackEnd part:
To better understand how to consume REST methods with Angular, we will set
up a small Spring Boot application that manages a simple entity called User.
We will start by implementing the UserRepository interface, followed by
creating the corresponding service, and finally implementing the web services
in the REST controller, as illustrated in the screenshots below.
1
Workshop concluded by: Sirine NAIFAR
Repository
Service
RestController
2
Workshop concluded by: Sirine NAIFAR
FrontEnd part:
➔ Note: To be able to inject HttpClient into this service, you need to import the
"HttpClientModule" module in the "app.module.ts" file. HttpClientModule is a
module in Angular that provides the HttpClient service for making HTTP
requests to remote servers. It must be imported into the root module of the
application or into a specific module if you want to use it in a specific part
of the application. Once imported, it can be injected into services to
perform CRUD operations and other types of HTTP requests.
3
Workshop concluded by: Sirine NAIFAR
Step 03: Consume a web service
In this part, you will learn and understand how to consume a web service in an
Angular 15 application. Starting with the method to retrieve users.
4
Workshop concluded by: Sirine NAIFAR
6- Call the "loadUsers" method in "ngOnInit()" to retrieve and display the list
of users when the "app.component.html" page is called
I- Add a user :
3- Import "FormBuilder" into your component (For this workshop, we will use
the app component) to create reactive forms:
5
Workshop concluded by: Sirine NAIFAR
6- In the component constructor, specify the composition of this form:
"firstName", "lastName", "birthDate", and "gender".
Validators: Angular provides several other predefined validators that you can
use to validate the fields of your form. Here are some of the most commonly
used validators :
6
Workshop concluded by: Sirine NAIFAR
You can combine these validators to define more complex validation rules for
your form fields. For example, to define both a minimum and maximum length
for a string field, you can use:
➔ This indicates that the "name" field is required, must have a minimum
length of 3 characters, and a maximum length of 30 characters.
7- Create the form in "app.component.html"
7
Workshop concluded by: Sirine NAIFAR
- (click)="cancelEdit()": This is a click event that calls the cancelEdit()
method in the component when the button is clicked. This cancels the
current edit.
8- Implement the "addUser()" and "cancelEdit()" methods in
"app.component.ts"
8
Workshop concluded by: Sirine NAIFAR
3- Implement the "editUser()" method to prefill the form with the user's data
to be edited
4- Modify the code of the table to be able to select the user to be edited:
9
Workshop concluded by: Sirine NAIFAR
5- Modify the code of the form in the "app.component.html" file :
10
Workshop concluded by: Sirine NAIFAR
3- Modify the code of the "user.component.html" class by adding a button
in the table to delete a user:
Final result :
11
Workshop concluded by: Sirine NAIFAR
Figure 2: Add user
12
Workshop concluded by: Sirine NAIFAR