Today i will show you how to create a service but first let’s talk about what is service. For example we want to use data in multiple component we will write logic in service to fetch data from server or API. We don’t have to write a different logic to fetch data from multiple time.
In this example iam using records.service.ts as a service and injected in app.module.ts file in provider and using this in two component with different data employeelist.component.ts and departmentlist.component.ts see the files below for more description.
Demo
app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { AppRoutingModule,routingComponents } from './app-routing.module';
import { RecordsService } from './records.service'
@NgModule({
declarations: [
AppComponent,
routingComponents
],
imports: [
BrowserModule,FormsModule,AppRoutingModule
],
providers: [RecordsService],
bootstrap: [AppComponent]
})
export class AppModule { }
departmentlist.component.ts
import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute, ParamMap} from '@angular/router';
import { RecordsService } from '../records.service'
@Component({
selector: 'app-departmentlist',
templateUrl: './departmentlist.component.html',
styleUrls: ['./departmentlist.component.css']
})
export class DepartmentlistComponent implements OnInit {
results = {}
constructor(private serviceData: RecordsService) { }
ngOnInit() {
this.results = this.serviceData.getResult();
}
}
departmentlist.component.html
<h3>
Getting data from the same service
</h3>
<ul class="list-group">
<li *ngFor="let result of results" class="list-group-item"> {{result.color}}</li>
</ul>
employeelist.component.ts
import { Component, OnInit } from '@angular/core';
import { RecordsService } from '../records.service'
@Component({
selector: 'app-employeelist',
templateUrl: './employeelist.component.html',
styleUrls: ['./employeelist.component.css']
})
export class EmployeelistComponent implements OnInit {
results = {}
constructor(private serviceData: RecordsService) { }
ngOnInit() {
this.results = this.serviceData.getResult();
}
}
employeelist.component.html
<h3>
Getting data from the same service
</h3>
<ul class="list-group">
<li *ngFor="let result of results" class="list-group-item"> {{result.value}}</li>
</ul>
records.service.ts
import { Injectable } from '@angular/core';
@Injectable()
export class RecordsService {
constructor() { }
getResult(){
return [
{
color: "red",
value: "#f00"
},
{
color: "green",
value: "#0f0"
},
{
color: "blue",
value: "#00f"
},
{
color: "cyan",
value: "#0ff"
},
{
color: "magenta",
value: "#f0f"
},
{
color: "yellow",
value: "#ff0"
},
{
color: "black",
value: "#000"
}
]
}
}
necdaph b9c45beda1 https://coub.com/stories/2648515-portable-free-download-quran-pak-with-urdu-translation-pdf
eliztam b9c45beda1 https://coub.com/stories/2630113-descargar-wondershare-winsuite-2012-gratis-best
carwebb b9c45beda1 https://coub.com/stories/2615717-physical-chemistry-silbey-solutions-manual-download-patched
ulapash 79a0ff67a5 https://coub.com/stories/2760429-rapidex-english-speaking-course-pdf-cracked-download-in-hindi
kassarie 79a0ff67a5 https://coub.com/stories/2805848-marina-tanya-oksana-olya-and-vica-11-14-y-o-from-russia-new
berkmic 79a0ff67a5 https://coub.com/stories/2617516-_top_-valya-36-18m37s-pthc-17
khrrah 4ea590b918 https://wakelet.com/wake/k5b3Dy9Zra8hnGgdWeZqJ
dailgerr ba0249fdb3 https://wakelet.com/wake/ChhOt1veTdTxrWcTXcUVU
heralas 90f3619eba https://coub.com/stories/2756064-new-kk9n0d18p-atok-2017-for-mac
mereliz 7383628160 https://coub.com/stories/2948304-battlefield-4-crack-v1-7-free-download-rodreem
talfab 7383628160 https://trello.com/c/EmiBbZbG/39-aci-sp-4-formwork-for-concrete-pdf-reinrac
blewen 7383628160 https://wakelet.com/wake/yySNXH5C2cZqv4DOscRrz
rosest fe98829e30 https://coub.com/stories/3077705-my-open-minded-wife-2006torrent-hit-reisar
othhiaw fe98829e30 https://coub.com/stories/3063370-fs2004-pmdg-747-400-premium-pack-torrent
tamaeldr fe98829e30 https://coub.com/stories/3071638-a-perfect-ending-assistir-online-legendado
carycun fe98829e30 https://www.cloudschool.org/activities/ahFzfmNsb3Vkc2Nob29sLWFwcHI5CxIEVXNlchiAgID_prm9CgwLEgZDb3Vyc2UYgICAn6u4lAoMCxIIQWN0aXZpdHkYgIDAoImM6ggMogEQNTcyODg4NTg4Mjc0ODkyOA
janpev fe98829e30 https://trello.com/c/N902OOZK/26-need-for-speed-nfs-payback-deluxe-edition-repack-mr-dj-version-download-link
karnisa fe98829e30 https://coub.com/stories/3112088-hd-online-player-kurukshetra-download-tamil-dubbed-mo-full
ademel fe98829e30 https://wakelet.com/wake/5Midi24pL0W_RyVq6UBld
elfcari d868ddde6e https://coub.com/stories/3002084-artcut2009parawindows7gratis-cracked
kaurbev d868ddde6e https://coub.com/stories/3075599-hd-songs-1080p-blu-ray-tamil-new-16-patched
martzyto d868ddde6e https://coub.com/stories/2950354-raspberry-pi-mpeg2-license-generator-hot-crack
jandxime d868ddde6e https://coub.com/stories/2936350-trigger-drum-steven-slate-torrent-top
dejakalo d868ddde6e https://coub.com/stories/3097642-pinnacle-studio-15-ultimate-collection-keygen-torr-gratis-geldgeschenk-farcar
matran d868ddde6e https://coub.com/stories/3099013-probability-and-queueing-theory-balaji-book-free-download-fidevan
godzede d868ddde6e https://coub.com/stories/3023663-dbx-to-pst-converter-7-4-crack-18-giaharm
ugolros d868ddde6e https://coub.com/stories/3032178-shiatafseerquraninurdufreedownload-better
wakyec d868ddde6e https://coub.com/stories/3084458-download-skenario-film-laskar-pelangi-hot
tamipeyt d868ddde6e https://coub.com/stories/3025416-exclusive-labeljoy-5-1-0-build-104
phemarc d868ddde6e https://coub.com/stories/3146429-pedagogy-in-urdu-pdf-olivquy
kaelfab d868ddde6e https://coub.com/stories/2940819-thief-of-baghdad-zee-tv-drama-top-full-torrent
jajasar d868ddde6e https://coub.com/stories/2971635-wdr-udma-5-3-rar-finnfear
salgua b7f02f1a74 https://westmisgobizsu.wixsite.com/roavedetalk/post/gp-5890x-iii-driver-epub
hamlkal b7f02f1a74 https://esinhescuadan.wixsite.com/calmiobamu/post/t-yerp-9-p1n0yak0-exe-torrent-pc-latest-license-patch
izalava b7f02f1a74 https://andrinalekberg036j.wixsite.com/xissosardist/post/32bit-isumsoft-office-password-refixer-3-1-1-activator-utorrent-pc
darnab c0c125f966 https://sumquobantingsulan.wixsite.com/meuvenpayfull/post/64bit-sm-fuck-indian-aunty-3gp-exe-utorrent-pc-software
beljaqu fb158acf10 https://benglimacarsi.wixsite.com/helphirtioxpow/post/movie-magic-scheduling-crack-license-key-full-latest
blagin fb158acf10 https://sialinsympgamil.wixsite.com/droolvipehor/post/spelling-riddles-like-eyes-rar-torrent-cracked-32bit-windows-activation
hallfati f4bc01c98b https://coub.com/stories/3441404-primer-express-software-version-3-0-free-download-top
gioelet f4bc01c98b https://coub.com/stories/3473164-don-no-1-south-indian-hindi-movie-download-nirvmaef
zandgeor f4bc01c98b https://coub.com/stories/3384225-fm-2010-patch-103-no-cd-crack-phimac
jaqdah f4bc01c98b https://coub.com/stories/3302654-__hot__-adobe-photoshop-cc-2017-v18-1-1-252-patch-mac-os-x
mahlweb f4bc01c98b https://coub.com/stories/3236491-dov-simens-film-school-dvd-torrentl-gloven