Introduction:
In this documentation, we will guide you through the process of integrating a GET API in an Angular application and displaying the retrieved records in a step-by-step manner. We'll provide code examples and explanations to help you understand the process.
Prerequisites:
Before we begin, ensure you have the following prerequisites:
- Node.js and npm installed on your system.
- Angular CLI installed globally.
Step 1: Create a New Angular Project
Open your terminal and run the following command to create a new Angular project:
bashng new angular-get-api-demo
Step 2: Set Up HTTP Client Module
Navigate to the newly created project directory:
bashcd angular-get-api-demo
Next, import the HttpClientModule in your app.module.ts
file:
typescriptimport { HttpClientModule } from '@angular/common/http';
@NgModule({
imports: [
// other modules...
HttpClientModule
],
// other configurations...
})
export class AppModule { }
Step 3: Create a Service to Fetch API Data
Generate a new service to handle API requests:
bashng generate service api
Replace the content of the api.service.ts
file with the following code:
typescriptimport { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class ApiService {
private apiUrl = 'https://api.example.com/data'; // Replace with your API endpoint URL
constructor(private http: HttpClient) { }
getRecords(): Observable<any> {
return this.http.get<any>(this.apiUrl);
}
}
Step 4: Display Data in a Component
Generate a new component to display the fetched data:
bashng generate component record-list
In the record-list.component.ts
file, add the following code:
typescriptimport { Component, OnInit } from '@angular/core';
import { ApiService } from '../api.service';
@Component({
selector: 'app-record-list',
templateUrl: './record-list.component.html',
styleUrls: ['./record-list.component.css']
})
export class RecordListComponent implements OnInit {
records: any[];
constructor(private apiService: ApiService) { }
ngOnInit(): void {
this.fetchRecords();
}
fetchRecords(): void {
this.apiService.getRecords().subscribe(
(data: any[]) => {
this.records = data;
},
(error) => {
console.error('Error fetching records:', error);
}
);
}
}
Step 5: Create the Template to Display Records
In the record-list.component.html
file, add the following code to display the records:
html<h2>Records:</h2>
<ul>
<li *ngFor="let record of records">
<!-- Display record properties here -->
</li>
</ul>
Step 6: Styling (Optional)
You can style the records list in the record-list.component.css
file as per your requirements.
Step 7: Display the Component
Finally, add the RecordListComponent
to your main app.component.html
file to see the records on the screen.
Conclusion:
Congratulations! You have successfully integrated a GET API in your Angular application and displayed the retrieved records. You can now customize and expand this implementation to suit your specific use case. Happy coding!
0 Comments