How To Call API In Angular
How To Call API And Get Data In Angular (PART-1)
How To Call API And Get Data In Angular (PART-2

In This Blog, you are going to learn how to call API and how to get data from an API and display it in a view File in our Angular App. Calling API in our applications is a must need sometimes.

Objectives

  • What is an API ?
  • How API Works In Angular ?
  • Understand HTTPClientModule
  • Create Service
  • Make a call to API
  • Display Data From API
What Is API ?
What Is API ?

What is an API ?

According to Wikipedia’s Definition of API: In computer programming, (API) is a set of subroutine definitions (functions), protocols, data and tools for building software and applications.

Workflow of API In Angular
Workflow of API In Angular
Getting API Data In Angular

Getting API Data In Angular

  • Every App must important to sending API request to another server. we can use http client request and get data and store data information to our server.
  • Specially when you are working with angular, vue, react application. you must have to learn how to run http client request with angular.
  • You can use this example with angular 8, angular 9, angular 10, angular 11, angular 12, angular 13 and angular 14 version.

1st Step: Create New App

You can easily create your angular app using bellow command:

ng new my-new-app

2nd Step: Import HttpClientModule

  • In this step, we need to import HttpClientModule to app.module.ts file. so let’s import it as like bellow:
    • src/app/app.module.ts

3rd Step: Create Service for API

Here, we need to create service for http client request. we will create service file and write client http request code. this service will use in our component file.

ng g s services/post

4th Step: Use Service to Component

  • Now we have to use this services to our app component.
    • src/app/app.component.ts

5th Step: Updated View File

  • Now here, we will updated our html file. let’s put bellow code:
    • src/app/app.component.html

Source Code

app.module.ts File Code – Importing HTTP Client Module

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import {HttpClientModule} from '@angular/common/http';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    HttpClientModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

post.service.ts File Code – Service File For API

import { Injectable } from '@angular/core';
import {HttpClient} from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})
export class PostService {
private url = 'http://jsonplaceholder.typicode.com/posts';
  constructor(private client:HttpClient) { }

  getPosts(){
    return this.client.get(this.url);
  }
}

app.component.ts File – Using Service in this app component

import { Component } from '@angular/core';
import { PostService } from './services/post.service';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'prj-api';
posts:any;

/**
 *
 */
constructor(private post:PostService) {}

ngOnInit(){
this.post.getPosts().subscribe(response => {
  this.posts = response;
  console.log(this.posts);
})
}

}

app.component.html File – Display API Data In HTML File

<h1>Getting Data From An API</h1>
<ol>
  <li *ngFor="let p of posts">
    {{ p.title }} -- {{p.id}}
  </li>
</ol>

Download Notes Of This Blog, From The Link Given Below.

https://www.mediafire.com/file/fxu4eskey2kv1hu/32+-+Get+API+Data.pptx/file

Download Source Code Of This Blog, From The Link Given Below.

https://www.mediafire.com/file/aixa9qcoiwve8bc/prj-api.rar/file

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *