Every front-end application needs to communicate with third party server to get and send data to server. Angular uses HTTP protocol to send and get data with server. Angular's HttpClient module helps to communicate with server.
In this example, we will create an angular application and send a get request to 3rd party server to get data and show over html. We will go through step by step. Let's start from creating new Angular application.
In the first step, we create a new Angular application using below ng command in Terminal or command prompt.
ng new request
This will create a new Angular project in workspace. Now go to the project directory with cd command.
cd request
Now open the application into your faviourite browser and open src/app
directory. All our new code files will be created in this directory. Create a new interface user.ts file and add the interface into it.
export interface User {
userId: number,
id: number,
title: string,
completed: boolean
}
We will use this interface to type cast our data.
Open app.module.ts
file and import HttpClientModule from @angular/platform-browser
directory. Also add it to imports array of @NgModule decorators.
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpClientModule } from '@angular/common/http';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule,
HttpClientModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
In angular, services are the class which sends request to server and send it component. Create a new service with following ng command:
ng generate service User
This will create a user.service.ts
file with UserService class which uses @Injectable decorators. Now in this service first import User interface and HttpClient module as below.
import { Injectable } from '@angular/core';
import { Observable, of } from 'rxjs';
import { HttpClient } from '@angular/common/http';
import { User } from './user';
@Injectable({
providedIn: 'root'
})
export class UserService {
private requestUrl = 'https://jsonplaceholder.typicode.com/todos/1';
constructor(
private http: HttpClient
) { }
getUser(): Observable<User> {
return this.http.get<User>(this.requestUrl);
}
}
In addition, we also create requestUrl property and object of HttpClient into constructor. getUser() method will call get() method from HttpClient object to requestUrl and return the data.
Now we will create a method into component. We will also create service object into constructor which will used to call service and receive data from HTTP request.
import { Component } from '@angular/core';
import { User } from './user';
import { UserService } from './user.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
user: any;
constructor (private userService: UserService) { }
ngOnInit(): void {
this.getUser();
}
getUser(): void {
this.userService.getUser()
.subscribe(user => this.user = user);
}
}
We have created user variable into component which is type of object so now we can use it into view file. So change app.component.html file as below:
<h1>User details</h1>
<p>userId: {{ user.userId }}</p>
<p>id: {{ user.id }}</p>
<p>title: {{ user.title }}</p>
<p>complete status: {{ user.completed }}</p>
Now we have completed the coding part. So it is time to check the request. In the Terminal, run the Angular server with following command:
ng serve --open
This will also open http://localhost:4200
in your default browser. The data from HTTP request will show as below:
This way, you can create and send HTTP get request to server and render data into HTML view. I hope you liked this article and it will help you in web development.
Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]
How to check if an element is hidden using jQuery
Use the jQuery :hidden Selector The j...How to Install Let's Encrypt on Ubuntu Server
While many web hosting providers provide...How to Secure File in Laravel Only Allow to Access Authenticated User
In this article, i will share with you h...PHP - Create file if it doesn’t already exist
In this article, we will use PHP to chec...Watch user location using Geolocation API
If you are developing real time user loc...