Angular 13 RxJS Observable with Httpclient Example
Hi,
Hello all! In this article, we will talk about angular 13 observable example. We will use angular 13 rxjs observable example. This tutorial will give you simple example of angular 13 http observable example. if you want to see example of angular 13 observable with httpclient example then you are a right place. Here, Creating a basic example of observable in angular 13 example.
If you don't know how to use observable with httpclient request in angular application then i will help you getting done. we always prefer to use observable for http request that help to manage server request and monitor to server request. observable is provided by rxjs.
Here, i will give you very simple example with http request with observable in angular. we will use jsonplaceholder api to make api request. so let's follow some step to get example done, i also attach preview on bottom.
Step 1: Create New App
You can easily create your angular app using bellow command:
ng new my-new-app
Step 2: 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
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { HttpClientModule } from '@angular/common/http';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
HttpClientModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Step 3: Create Post Class
In this step, we will simply create Post class and define data types of returning data. so let's create post.ts file and put bellow code:
src/app/post.ts
export class Post {
constructor(
public body: string,
public id: number,
public title: string,
public userId: number
) {}
}
Step 4: Create Service for Call API
Here, we need to create service for http client request. we will create service file and write client http request using observable code. this service will use in our component file. So let's create service and put bellow code:
ng g s post
Now let's add code as like bellow:
src/app/post.service.ts
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { Post } from './post';
@Injectable({
providedIn: 'root'
})
export class PostService {
private url: string = 'https://jsonplaceholder.typicode.com/posts';
constructor(private httpClient: HttpClient) { }
public getPosts(): Observable<Post[]>{
return this.httpClient.get<Post[]>(this.url);
}
}
Step 5: Use Service to Component
Now we have to use this services to our app component. So let's updated code as like bellow:
src/app/app.component.ts
import { Component, OnInit } from '@angular/core';
import { PostService } from './post.service';
import { Post } from './post';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
name = 'Angular';
posts = new Array<Post>();
constructor(private service:PostService) {}
ngOnInit() {
this.service.getPosts().subscribe(response => {
this.posts = response.map(item =>
{
return new Post(
item.body,
item.id,
item.title,
item.userId
);
});
});
}
}
Step 6: Updated View File
Now here, we will updated our html file. let's put bellow code:
I used bootstrap class on this form. if you want to add than then follow this link too: Install Bootstrap 5 to Angular 13.
src/app/app.component.html
<div class="container">
<h1>Angular 13 Observables HttpClient Example - ItSolutionStuff.com</h1>
<table class="table table-bordered">
<tr>
<th>ID</th>
<th>Body</th>
<th>Title</th>
<th>UserID</th>
</tr>
<tr *ngFor="let post of posts">
<td>{{ post.id }}</td>
<td>{{ post.body }}</td>
<td>{{ post.title }}</td>
<td>{{ post.userId }}</td>
</tr>
</table>
</div>
Run Angular App:
All the required steps have been done, now you have to type the given below command and hit enter to run the Angular app:
ng serve
Now, Go to your web browser, type the given URL and view the app output:
http://localhost:4200
You can simply see preview as like bellow:
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Angular 13 HttpClient & Http Services Tutorial Example
- Angular 13 Bootstrap Datepicker Example
- Angular 13 Material Datepicker Tutorial Example
- How to Setup Routing & Navigation in Angular 13?
- Angular 13 CRUD Application Example Tutorial
- Angular 13 Multiple File Upload Tutorial
- Angular 13 Template Driven Forms with Validation Example
- Angular 13 Bootstrap Modal Popup Example
- Angular 13 Multiple Image Upload with Preview Tutorial
- Angular 13 Image Upload with Preview Tutorial
- Angular 13 Reactive Forms Validation Tutorial Example