How To Perform Get() Operation Using In-Memory Web API And Service In Angular

Angular In-Memory web API

 
Angular in-memory web API is a library that communicates angular HTTP and the angular HTTP client request. That means angular in-memory web API is a remote server that stores the value and redirects the data that you control into the frontend.
 
In-memory web API is used to test the Angular application's demos. Otherwise, you have to use the remote server to handle the applications.
 
To test our application we need a web service URL. Angular provides an in-memory web API that will provide a web service URL. We can configure URLs with dummy data using in-memory web API. Find the step angular in-memory web API.
 
To install Angular in-memory web API you have to follow a few steps.
 
Step 1
 
To create an in-memory web API you have to create an application and after creating it successfully you have to open the terminal.
 
Step 2
 
Now run the command for installing in-memory web API. “npm install in-memory web API -- save”. And after completing this process, you have to create a file like testdata.ts
 
Step 3
 
Now go to this file and import the in-memory web from the Angular library. Then you have to create a function with the name of “created”.
 
Step 4
 
Then get a variable with the name of book details that when typed is declared as an array.
 
Step 5
 
Add some details in the array and close this array.
 
Now you have to return this variable in the database body.
 
Now your API will be created and you will use this API in the service. 
 

How to use in-memory web API in the Angular service

 
Step 1
 
Now you have to create a service using the command “ ng g service class name ”
 
Step 2
 
After creating this successfully  you have to open the service.ts file.
 
Step 3
 
First you have to import observable and HTTP clients from the Angular library.
 
Httpclient import from the angular/common/HTTP library. And observable is an import from the rxjs-library.
 
Step 4
 
Now you have to declare a variable(type any or array[]).
 
Step 5
 
Now you have to create a URL of your API. And import this file path.
 
Step 6
 
Now you have to go to the constructor and create a private HTTP variable.
 
Step 7
 
Now create a function get-book (){} and then take an observable and then take the observable type and then you have to return this HTTP method and then get the URLs which we created in the service file.
 
Now you have to create a .ts file and then you have to go to the class and declare all variables which  you have to take in the .api data. 
 
Then you have to import this file into the service file and create an object in the service file.
 
Now your service is created successfully.  Now you will use this service in your application. 
 

Use this service in Angular applications

 
Step 1
 
Now you have to go to your .ts file and create import the wrapper class and create an object.
 
Step 2
 
Then you have to import this service.
 
Step 3
 
Then you have to go to the constructor section and let private HTTP for the service.
 
Step 4
 
Then you have to create a function for the query.
 
Step 5
 
Then you have to return this function.
 
Step 6
 
Create the query for showing the data. 
 
Step 7
 
Now you have to open the show.html file. and create a list.
 
Step 8
 
Now you have to use the for loop in the list and define all values which value you are showing in the list item.
 
Step 9
 
Now you have to go to the module files and import all components, services, and all dependencies, and register all the requirements and then you have to run the applications. After running the application you have to see that all required details are shown in the browser.
 

Example of in-memory web API

 
First, run the command,
  1. npm install in-memory web API -- save  
This process will take few seconds to completely install. 
 
Now write the code in the api.ts file.
 
api.ts
  1. import{InMemoryDbService} from 'angular-in-memory-web-api'  
  2. export class TestData implements InMemoryDbService{  
  3.     createDb(){  
  4.   
  5.         let BookDetails=[  
  6.             {id:100, name:'chaman gautam', Author:'C#corner',category:'Software devcelopment'},  
  7.             {id:101, name:'lucky gautam', Author:'Tpoint',category:'test'},  
  8.             {id:102, name:'Lakshya gautam', Author:'Google',category:' devcelopment'},  
  9.             {id:103, name:'ravi gautam', Author:'C#corner',category:'Software devcelopment'}  
  10.              
  11.         ];  
  12.         return {  
  13.             books:BookDetails  
  14.         };  
  15.     }  
  16.   
  17. }  
service.ts
  1. import { Injectable } from '@angular/core';  
  2. import {HttpClient, HttpHeaders} from '@angular/common/http';  
  3. import{Observable } from 'rxjs';  
  4. import { Book } from './book';  
  5. @Injectable({  
  6.   providedIn: 'root'  
  7. })  
  8. export class BookService {  
  9. bookUrl="api/books"  
  10.   
  11.   constructor( private http:HttpClient) { }  
  12.   
  13.   createbook(book:Book):Observable<Book>{  
  14.     let httpheaders=new HttpHeaders()  
  15.     .set('Content-type','application/Json');  
  16.     let options={  
  17.       headers:httpheaders  
  18.     };  
  19.     return this.http.post<Book>(this.bookUrl,book,options);  
  20.   }  
  21.   
  22.   getBooksFromStore():Observable<Book[]>{  
  23.     return this.http.get<Book[]>(this.bookUrl);  
  24.   }  
  25. }  
rapper.ts
  1. export interface Book{  
  2. id:number;  
  3. name:string;  
  4. Author:string;  
  5. catogery:string  
  6. }  
showdata.ts
  1. import { Component, OnInit } from '@angular/core';  
  2. import { BookService } from '../book.service';  
  3. import{ Book} from '../book'  
  4.   
  5. @Component({  
  6.   selector: 'app-showdata',  
  7.   templateUrl: './showdata.component.html',  
  8.   styleUrls: ['./showdata.component.css']  
  9. })  
  10. export class ShowdataComponent implements OnInit {  
  11.   
  12.   title='chaman gautam';  
  13.   softBooks:Book[];  
  14.   constructor(private bookservice:BookService) { }  
  15.   
  16.   ngOnInit(){  
  17.   
  18.     this.getsoftBooks();  
  19.   
  20.   }  
  21. getsoftBooks(){  
  22.   this.bookservice.getBooksFromStore().subscribe(books => this.softBooks=books);  
  23. }  
  24.   
  25. }  
showdata.html
  1. <h1><p>Show data using inmemory web api</p></h1>  
  2.   
  3. <ul>  
  4.     <li *ngFor="let bk of softBooks">  
  5.   
  6.         Id     {{bk.id}},     Name:  {{bk.name}},  Author  {{bk.Author}},   Category:  {{bk.category}}  
  7.     </li>  
  8. </ul>  
module.ts
  1. import { NgModule } from '@angular/core';  
  2. import { BrowserModule } from '@angular/platform-browser';  
  3.   
  4. import { AppRoutingModule } from './app-routing.module';  
  5. import {HttpClientModule } from '@angular/common/http'  
  6. import { AppComponent } from './app.component';  
  7. import { AddComponent } from './add/add.component';  
  8. import { RouterModule, Routes} from '@angular/router';  
  9. import{BookService } from './book.service';  
  10. import {InMemoryWebApiModule } from 'angular-in-memory-web-api';  
  11. import {TestData } from './testdata'  
  12.   
  13. import { FormsModule, ReactiveFormsModule } from '@angular/forms';  
  14. import { ShowdataComponent } from './showdata/showdata.component';  
  15. import { CommonModule } from '@angular/common';  
  16. const routes: Routes = [  
  17.   {path:'', component:ShowdataComponent},  
  18.   { path:'add', component:AddComponent}  
  19. ];  
  20.   
  21. @NgModule({  
  22.   declarations: [  
  23.     AppComponent,  
  24.     AddComponent,  
  25.     ShowdataComponent  
  26.   ],  
  27.   imports: [  
  28.     BrowserModule,  
  29.     AppRoutingModule,RouterModule.forRoot(routes),ReactiveFormsModule, FormsModule,  
  30.     HttpClientModule, CommonModule,  
  31.     InMemoryWebApiModule.forRoot(TestData)  
  32.   ],  
  33.   providers: [BookService],  
  34.   bootstrap: [ShowdataComponent]  
  35. })  
  36. export class AppModule { }  
Now run this project using the command " ng serve ". and after compiling successfully you have to open the browser and hit " localhost:4200". after a few second your data will be shown like below. 
 
OUTPUT 
 
 
I hope you enjoyed this article. Follow Chaman Gautam to learn more about Angular, and follow C# Corner to to learn about more technology.