Object List With Services Using Angular

Angular is a platform that makes it easy to build applications with the web. In Angular Architecture, Services are used for reusable data to be shared between components throughout an application. Services are mainly used for HTTP Calls.

Components shouldn’t fetch or save the data directly and they certainly shouldn’t knowingly present the fake data. They should focus on presenting the data and delegate data access to a service.

Here, we are going to use an objects list with services in Angular 5 and will display these entities in a table. Please go through the following steps.

Create New Project

Create a new folder named Angular5-ObjectList and select this folder in Visual Studio Code.

Install Angular 5

Open the terminal window in Visual Studio Code and type the following command.

npm install -g @angular/cli
 
Object List with Services Using Angular 

Create Entity

Create a new folder named "entities" in src\appfolder. In this folder, create a new file named "ngconflocation.entity.ts" that contains the product information as below.

  1. export class NgConfLocation {  
  2.    id: string;  
  3.    location: string;  
  4.    organizer: string;  
  5.    Image: string;  
  6. }  

Create Service

Create a new folder named "services" in src\appfolder. In this folder, create a new file named "ngconf.service.ts" as below.

  1. import { Injectable } from '@angular/core';  
  2. import { NgConfLocation } from '../entities/ngconflocation.entity';  
  3.   
  4. @Injectable ({  
  5.   providedIn: 'root'  
  6. })  
  7. export class NgconfService {  
  8.   
  9.   findall(): NgConfLocation [] {  
  10.   return [  
  11.     {  
  12.    id: 'Ng01',  
  13.    location : 'India',  
  14.    organizer: 'Dhananjay Kumar',  
  15.    image: 'ngindia.PNG'  
  16.   },  
  17.   {  
  18.     Id: 'Ng02',  
  19.     location : 'Poland',  
  20.     organizer: 'Dariusz Kalbarczyk',  
  21.     image: 'ngpoland.PNG'  
  22.    },  
  23.    {  
  24.     id: 'Ng03',  
  25.     location : 'London',  
  26.     organizer: 'Josh Moont',  
  27.     image: 'Capture.PNG'  
  28.    }  
  29.   ];  
  30.   }  
  31.   
  32. }  

Create Component

Create a new file named "app.component.ts" in src\app folder. In this component, we will call the methods in "ngconf service".

  1. import { Component , OnInit} from '@angular/core';  
  2. import { NgConfLocation } from './entities/ngconflocation.entity';  
  3. import { NgconfService } from './services/ngconf.service';  
  4.   
  5. @Component({  
  6.   selector: 'app-root',  
  7.   templateUrl: './app.component.html',  
  8.   styleUrls: ['./app.component.css']  
  9. })  
  10. export class AppComponent implements OnInit {  
  11.     ngconf: NgConfLocation[];  
  12.   constructor (private ngconflocService: NgconfService) {  
  13.   
  14.   }  
  15.   ngOnInit() {  
  16. this.ngconf = this.ngconflocService.findall();  
  17.   }  
  18. }  

Create View

Create a new file, "app.component.html" in src\app folder. In this View, show the values from the component.

  1. <table  border="1" align='center'width="50"  >  
  2.     <thead >  
  3.       <tr>  
  4.         <th scope="col">NG-ID</th>  
  5.         <th scope="col">Location</th>  
  6.         <th scope="col">Organizer</th>  
  7.         <th scope="col" >Logo </th>  
  8.       </tr>  
  9.     </thead>  
  10.     <tbody>  
  11.             <tr *ngFor="let p of ngconf">   
  12.               <td>{{p.id}}</td>   
  13.               <td>{{p.location}}</td>   
  14.               <td>{{p.organizer}}</td>   
  15.               <td> <img src="assets/images/{{p.image}}" width="150"  > </td>   
  16.               </tr>  
  17.               <tr>  
  18.             </tbody>  
  19.           </table>  

Create CSS

  1. th, td {  
  2.     padding: 15px;  
  3.     text-align: left;  
  4.   }  
  5.   table, td, th {    
  6.     border: 1px solid #ddd;  
  7.     text-align: left;  
  8.   }  
  9.    th {  
  10.     padding-top: 12px;  
  11.     padding-bottom: 12px;  
  12.     text-align: left;  
  13.     background-color: rgb(172, 76, 175);  
  14.     color: white;  
  15.   }  
  16.   table {  
  17.     border-collapse: collapse;  
  18.     width: 50%;  
  19.       
  20.   }  
  21.   tr:hover {background-color: rgb(100, 123, 136);  
  22. }  

Add Component to Module

In app.module.ts file in src\app folder, add a new component to the module.

  1. import { BrowserModule } from '@angular/platform-browser';  
  2. import { NgModule } from '@angular/core';  
  3. import { AppRoutingModule } from './app-routing.module';  
  4. import { AppComponent } from './app.component';  
  5. import { NgconfService } from './services/ngconf.service';  
  6. import {FormsModule} from '@angular/forms';  
  7.   
  8. @NgModule({  
  9.   declarations: [  
  10.     AppComponent  
  11.   ],  
  12.   imports: [  
  13.     BrowserModule,  
  14.     AppRoutingModule,  
  15.     FormsModule  
  16.   ],  
  17.   providers: [NgconfService],  
  18.   bootstrap: [AppComponent]  
  19. })  
  20. export class AppModule { }  

Run Application

In terminal window of Visual Studio Code, type the following command

ng serve --open
 
The program will open a URL http://localhost:4200/ in the browser.
 
Object List with Services Using Angular