{"id":19091,"date":"2017-11-08T12:15:04","date_gmt":"2017-11-08T10:15:04","guid":{"rendered":"https:\/\/www.webcodegeeks.com\/?p=19091"},"modified":"2017-11-06T11:12:34","modified_gmt":"2017-11-06T09:12:34","slug":"angular-4-service-example","status":"publish","type":"post","link":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/angular-4-service-example\/","title":{"rendered":"Angular 4: Service example"},"content":{"rendered":"<p>Welcome to Angular 4 services<\/p>\n<p>Angular services are for building out code that can be shared across multiple components. And these are singletons. services restrict the instantiation of a class to one object. It means the methods and properties can be shared throughout your application from a single shared instance.<\/p>\n<p>These services are best way to store the session data and perfect place to call the web services. And the data is shared to the components with the injecting service. Without hitting the web services we can use the session data in the application. But If you refresh the browser the session service data will be empty. So remember that condition.<\/p>\n<p>In this tutorial, we provide a basic angular 2 service example using the Angular CLI.<\/p>\n<p>We\u2019ll be using the\u00a0Angular CLI\u00a0to create our app. Create a new Angular project by running:<\/p>\n<blockquote><p><strong>ng new AngularServiceEx<\/strong><\/p><\/blockquote>\n<p>This will create the new project named as AngularServiceEx project.<\/p>\n<p>And navigate to root application by <strong>cd<\/strong>\u00a0 <strong>AngularServiceEx. <\/strong>And run the below command<\/p>\n<blockquote><p><strong>Npm install<\/strong><\/p><\/blockquote>\n<p>This command will download all your dependencies from your package.json file.<\/p>\n<p>And run the below command to create the service in the project.<\/p>\n<blockquote><p><strong>ng g service reference<\/strong><\/p><\/blockquote>\n<p>It will create the reference.service in the \u2019app\u2019 folder automatically. We don\u2019t need to do anything.\u00a0 It will create the two files in our Angular app<\/p>\n<ul>\n<li>a\u00a0reference.service.tsfile under the src\/app directory<\/li>\n<li>a\u00a0reference.service.spec.tsfile under the src\/app directory<\/li>\n<\/ul>\n<p>We will write the service code in the service.ts file.\u00a0 And service.spec.ts file will used for unit testing.<\/p>\n<p>The\u00a0ng g service\u00a0command won\u2019t automatically configure your root module\u00a0app.module.ts\u00a0to provide the service.<\/p>\n<p>Services must import the Injectable from the angular\/core package, like below. see the <a href=\"https:\/\/angular.io\/tutorial\/toh-pt4\">angular. io<\/a> website for more info on services.<\/p>\n<pre class=\"brush:java\">import { Injectable } from \u2018@angular\/core\u2019;<\/pre>\n<p>And services uses the\u00a0 injectable decorator. <strong>@Injectable()<\/strong> . The @Injectable decorator simply provides dependency injection for the service.<\/p>\n<p>Don\u2019t forget to add the decorator in the service.ts file. If we don\u2019t add it will not work.<\/p>\n<p>Service must have the export keyword. \u00a0Everything like class or component should use \u2018export\u2019\u00a0 keyword. Then only it is possible to use the class or component or anything in the project. And service name should end with .service. \u00a0For ex:\u00a0 reference.service.ts file. If we have lengthy name we need add the \u2018\u2013\u2018 in the service name. That is for code readability.<\/p>\n<p>Example: save-user-info.service.ts<\/p>\n<p><strong><u>Service structure:<\/u><\/strong><\/p>\n<pre class=\"brush:java\">import { Injectable } from '@angular\/core';\r\n\r\n@Injectable()\r\n\r\nexport class ReferenceService {\r\n\r\nconstructor() {\u00a0 console.log(\u201cwelcome! Reference Service\u201d);\u00a0 }\r\n\r\n}}<\/pre>\n<p>To include the service in your app, you must import it and include it as a provider in\u00a0app.module.ts or we can add the service in providers of component meta data also. But it will be available in the particular component only. But it\u2019s better to use the service in the providers of app.module.ts. Service will create the instance every time, whenever you come to the component. \u00a0By registering the service as a provider, you make it available everywhere in the application.<\/p>\n<p><strong><u>app.module.ts<\/u><\/strong><\/p>\n<pre class=\"brush:java\">import { BrowserModule } from '@angular\/platform-browser';\r\nimport { NgModule } from '@angular\/core';\r\nimport { AppComponent } from '.\/app.component';\r\nimport { ReferenceService } from '.\/reference.service'\r\n\r\n@NgModule({\r\ndeclarations: [ AppComponent ],\r\nimports: [ BrowserModule ],\r\nproviders: [ReferenceService],\r\nbootstrap: [AppComponent]\r\n})\r\n\r\nexport class AppModule { }<\/pre>\n<p><u>Accessing the Service from a component: <\/u><\/p>\n<p><strong><u>app.component.ts<\/u><\/strong><\/p>\n<p>Services must be imported by the component. We will import our reference service in open the auto-generated\u00a0app.component.ts\u00a0and replace it with the following:<\/p>\n<pre class=\"brush:java\">import { Component } from '@angular\/core';\r\nimport { ReferenceService } from '.\/reference.service'\r\n\r\n@Component({\r\nselector: 'app-root',\r\ntemplateUrl: '.\/app.component.html',\r\nstyleUrls: ['.\/app.component.css']\r\n})\r\n\r\nexport class AppComponent {\r\nconstructor(public reference: ReferenceService){\r\n\r\n}\r\n}<\/pre>\n<p><strong><u>Service with HTTP methods :<\/u><\/strong><\/p>\n<p>We will have to interact with Database to get the data and post the data. For that we will write the web service api in the Angular services. We will have to call the HTTP POST or HTTP GET methods with REST API in the service to store that data in the DATABASE or retrieve the data from the DATABASE.<\/p>\n<p><strong>Let\u2019s see the Example with real Json file<\/strong>:<\/p>\n<p>We will use the Reference Service. And we will write the http method in the reference service. That http method should be called in the component methods to get the data or post the data to the REST side. For now will go through with angular services with Json file.Will add one json file in the project. In the angular every request and response will be in the Json \u00a0only. You need to add the json file in the assets folder under src folder of your project. You may get 404 json file not found, if you add the json file somewhere other than assets location folder.<\/p>\n<p><strong><u>Json file:<\/u><\/strong><\/p>\n<p>Created the json folder in the assets folder in \u2018src\/assets\u2019. See example json data.<\/p>\n<pre class=\"brush:js\">[\r\n\r\n{ \u201cid\u201d: 10, \"firstName\":&lt;em&gt;\"Sathish\"&lt;\/em&gt;, \"lastName\":&lt;em&gt;\"Kotha\"&lt;\/em&gt; },\r\n\r\n{ \u201cid\u201d: 11, \"firstName\":&lt;em&gt;\"Ramesh\"&lt;\/em&gt;, \"lastName\":&lt;em&gt;\"Kotha\"&lt;\/em&gt; },\r\n\r\n{ \u201cid\u201d: 12, \"firstName\":&lt;em&gt;\"Ragav\"&lt;\/em&gt;, \"lastName\":&lt;em&gt;\"Raju\"&lt;\/em&gt; }\r\n\r\n]<\/pre>\n<p><strong><u>Employee class: <\/u><\/strong><\/p>\n<p>We need to create the Employee model class. It\u2019s a type of json data, remember every where we need export the class. This model class is a typescript format. Type the below command.<\/p>\n<blockquote><p><strong>ng g class Employee<\/strong><\/p><\/blockquote>\n<p>This will create the Employee model class in the app folder.<\/p>\n<p>typically you declare types in a model which can then be reused throughout the rest of the application. With TypeScript, frontend applications can now benefit from strongly typed models!<\/p>\n<p>This is pretty straightforward for declaring individual variables,<\/p>\n<p>A simple example of this is a\u00a0Employee class that defines a\u00a0firstName and lastName\u00a0variables that\u2019s are string and an\u00a0id variable that must be a number:<\/p>\n<pre class=\"brush:java\">export class Employee\u00a0 {\r\n\r\nid: number;\r\n\r\nfirstName: string;\r\n\r\nlastName: string;\r\n\r\n}<\/pre>\n<p><strong><u>Service:<\/u><\/strong><\/p>\n<p>In the service, we will \u00a0import the Http from \u201c@angular\/http\u201d; with http we will use the http get the post and request methods. To get the all the methods we need to include the http in the constructor. for more info on http please refer <a href=\"https:\/\/angular.io\/tutorial\/toh-pt6\">https:\/\/angular.io\/tutorial\/toh-pt6<\/a><\/p>\n<p>And import some rxjs. These are reactive extension java script. Rxjs is a reactive streams library that allows you to work with\u00a0<em>asynchronous data streams. <\/em>RxJS can be used both in the browser or in the server-side using Node.js. We will use some methods from the Rxjs in angular services like map and observable and catch. We will use these in the services.<\/p>\n<p>Don\u2019t instantiated the service with <strong>new<\/strong> keyword, it will create error prone and memory leak.<\/p>\n<pre class=\"brush:java\">import { Injectable } from '@angular\/core';\r\n\r\nimport { Http } from '@angular\/http';\r\n\r\nimport 'rxjs\/add\/operator\/map';\r\n\r\nimport 'rxjs\/add\/operator\/catch';\r\n\r\nimport { Observable } from 'rxjs\/Observable';\r\n\r\nimport { Employee } from '.\/employee';\r\n\r\n@Injectable()\r\n\r\nexport class ReferenceService {\r\n\r\nconstructor(private http: Http) { }\r\n\r\ngetAllEmployees(): Observable&lt; Employee &gt; {\r\n\r\ntry {\r\n\r\nreturn this.http.get(\u2018assets\/service.json') .map(this.extractdata).catch(this.handleerror) ;\r\n\r\n\/\/ Api Full path like, http:\/\/localhost:4200\/assets\/service.json\r\n\r\n} \u00a0\u00a0catch (error) { console.log(error); }\u00a0 }\r\n\r\nextractData(res: Response) {\r\n\r\nconst body = res.json();\r\n\r\nreturn body || {};\u00a0\u00a0 }\r\n\r\nhandleError(error: any) { return Observable.throw(error);\u00a0}\r\n}<\/pre>\n<p>In the above service\u00a0 http reference is used to call the http methods. We created one method is <strong>get all employees<\/strong>, we are calling http.get method with json link. This get method will get the json data. Then it will come to \u00a0map\u00a0method, it is used to extract the JSON content from the response coming as the observable type. if you got any error while calling the http method it will come to catch method,from there it will directly throw the error with observable throw method.<\/p>\n<p><strong><u>Component:<\/u><\/strong><\/p>\n<p>Import the reference service in the component as below and one more thing we need the ngOn Init life cycle method. Every component has to implement the ngOnInit method. And this method will call after the constructor. It\u2019s used like a main method. When you come to component, ng On Init method will call, you can call any number of methods in the ngOnInit method.<\/p>\n<pre class=\"brush:java\">import { Component, OnInit } from '@angular\/core';\r\n\r\nimport { ReferenceService } from '.\/reference.service';\r\n\r\nimport { Employee } from '.\/employee';\r\n\r\n@Component({\r\n\r\nselector: 'app-root',\r\n\r\ntemplateUrl: '.\/app.component.html',\r\n\r\nstyleUrls: ['.\/app.component.css']\u00a0 })\r\n\r\nexport class AppComponent implements OnInit\u00a0 {\r\n\r\ntitle = 'Angular Service app';\r\n\r\nemployeesInfo: any;\r\n\r\nconstructor(public referService: ReferenceService) { }\r\n\r\ngetAllEmployeesInfo() {\r\n\r\nthis.referService.getAllEmployees() .subscribe((result: Employee[]) =&gt; {\r\n\r\nconsole.log('the employess data:' + result);\r\n\r\nthis.employeesInfo = result;\u00a0\u00a0\u00a0\u00a0\u00a0 },\r\n\r\n(error: any) =&gt; {\u00a0 console.log('error in employees method');\u00a0\u00a0\u00a0\u00a0\u00a0 });\u00a0\u00a0 }\r\n\r\nngOnInit() {\u00a0 this. getAllEmployeesInfo();\u00a0 }\r\n\r\n}<\/pre>\n<p>In above component, service method is called in component method getAllEmployeesInfo with reference service http method. And when we got the output it will come to subscribe result.\u00a0 There we are assigning the result to our variables. If we get any error when calling the api it will come to error block. This error will return from catch of Observable \u00a0throw error.<\/p>\n<p><strong><u>App.component.html:<\/u><\/strong><\/p>\n<pre class=\"brush:xml\">&lt;h1 style=\"text-align: center\"&gt;\u00a0 Welcome to {{title}}! &lt;\/h1&gt;\r\n\r\n&lt;h3 style=\"text-align: center\"&gt;ALL employeesInfo here&lt;\/h3&gt;\r\n&lt;div style=\"text-align: center;\"&gt;\r\n\r\n&lt;b&gt;Emp ID&lt;\/b&gt; {{emp.id}}\r\n\r\n&lt;b&gt;\u00a0 First Name is :&lt;\/b&gt; {{emp.firstName}}\r\n\r\n&lt;b&gt;Last Name is :&lt;\/b&gt; {{emp.lastName}}\r\n\r\n&lt;\/div&gt;<\/pre>\n<p>Above html we are using title in curly brackets. It\u2019s a two way bindings. Whatever we add it automatically convert the value. And one more new word is \u201c*ngFor\u201d structural directives. This is used to iterate the json data in the html. We have the multiple employes the data in the \u201c employessInfo\u201d\u00a0 variable. So we need to include the ng for here to iterate the data in the html. We add the reference value in the ng for. With that will call the inside object data.<\/p>\n<p><strong><u>Service used in the Module:<\/u><\/strong><\/p>\n<pre class=\"brush:java\">import { BrowserModule } from '@angular\/platform-browser';\r\n\r\nimport { NgModule } from '@angular\/core';\r\n\r\nimport { HttpModule } from '@angular\/http';\r\n\r\nimport { AppComponent } from '.\/app.component';\r\n\r\nimport { ReferenceService } from '.\/services\/reference.service';\r\n\r\n@NgModule({\r\n\r\ndeclarations: [ AppComponent ],\r\n\r\nimports: [ BrowserModule, HttpModule ],\r\n\r\nproviders: [ReferenceService],\r\n\r\nbootstrap: [AppComponent] })\r\n\r\nexport class AppModule { }<\/pre>\n<p>Above app.module we have imported reference service and app component , All components should be added in the declarations field. And service should go to provides. Httmodule is also imported to imports. And last app component will be added in the bootstrap , bootstrap will added to project from here<\/p>\n<p>if you don\u2019t add the service in the providers you will get following error. One more thing we need to add the http module in the app module imports. Don\u2019t add the http in the imports. If you don\u2019t include. You will get below error.<\/p>\n<p>EXCEPTION: No provider for service!<\/p>\n<p><strong>Clone the repository and install all it\u2019s dependencies:<\/strong><\/p>\n<p># Clone repo<\/p>\n<p>git clone <a href=\"https:\/\/github.com\/Sathishchary\/AngularServiceEx\" rel=\"nofollow\">https:\/\/github.com\/Sathishchary\/AngularServiceEx<\/a><\/p>\n<p># Enter into directory<\/p>\n<p>cd AngularServiceEx<\/p>\n<p># Install dependencies<\/p>\n<p>npm install<\/p>\n<div class=\"attribution\">\n<table>\n<tbody>\n<tr>\n<td>Published on Web Code Geeks with permission by Ramesh Kotha, partner at our <a href=\"http:\/\/www.webcodegeeks.com\/join-us\/wcg\/\" target=\"_blank\" rel=\"noopener\">WCG program<\/a>. See the original article here: <a href=\"https:\/\/java2practice.com\/2017\/11\/03\/angular-4-service-example\/\" target=\"_blank\" rel=\"noopener\">Angular 4: Service example<\/a><\/p>\n<p>Opinions expressed by Web Code Geeks contributors are their own.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Welcome to Angular 4 services Angular services are for building out code that can be shared across multiple components. And these are singletons. services restrict the instantiation of a class to one object. It means the methods and properties can be shared throughout your application from a single shared instance. These services are best way &hellip;<\/p>\n","protected":false},"author":1516,"featured_media":909,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[],"class_list":["post-19091","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-angular-js"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Angular 4: Service example - Web Code Geeks - 2026<\/title>\n<meta name=\"description\" content=\"Welcome to Angular 4 services Angular services are for building out code that can be shared across multiple components. And these are singletons. services\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/angular-4-service-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Angular 4: Service example - Web Code Geeks - 2026\" \/>\n<meta property=\"og:description\" content=\"Welcome to Angular 4 services Angular services are for building out code that can be shared across multiple components. And these are singletons. services\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/angular-4-service-example\/\" \/>\n<meta property=\"og:site_name\" content=\"Web Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webcodegeeks\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/Kotha.Ramesh\" \/>\n<meta property=\"article:published_time\" content=\"2017-11-08T10:15:04+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/angularjs-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Ramesh Kotha\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@rameshchary\" \/>\n<meta name=\"twitter:site\" content=\"@webcodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ramesh Kotha\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/angular-4-service-example\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/angular-4-service-example\/\"},\"author\":{\"name\":\"Ramesh Kotha\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/ad5479e29af7e3c3a3295a5ff859221d\"},\"headline\":\"Angular 4: Service example\",\"datePublished\":\"2017-11-08T10:15:04+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/angular-4-service-example\/\"},\"wordCount\":1462,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/angular-4-service-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/angularjs-logo.jpg\",\"articleSection\":[\"Angular.js\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/angular-4-service-example\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/angular-4-service-example\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/angular-4-service-example\/\",\"name\":\"Angular 4: Service example - Web Code Geeks - 2026\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/angular-4-service-example\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/angular-4-service-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/angularjs-logo.jpg\",\"datePublished\":\"2017-11-08T10:15:04+00:00\",\"description\":\"Welcome to Angular 4 services Angular services are for building out code that can be shared across multiple components. And these are singletons. services\",\"breadcrumb\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/angular-4-service-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/angular-4-service-example\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/angular-4-service-example\/#primaryimage\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/angularjs-logo.jpg\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/angularjs-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/angular-4-service-example\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.webcodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript\",\"item\":\"https:\/\/www.webcodegeeks.com\/category\/javascript\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Angular.js\",\"item\":\"https:\/\/www.webcodegeeks.com\/category\/javascript\/angular-js\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Angular 4: Service example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"name\":\"Web Code Geeks\",\"description\":\"Web Developers Resource Center\",\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.webcodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webcodegeeks\",\"https:\/\/x.com\/webcodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/ad5479e29af7e3c3a3295a5ff859221d\",\"name\":\"Ramesh Kotha\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/7fb4cc19e57a57b2663f160acad270a3831bf7235310c5536163c89114eeb479?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/7fb4cc19e57a57b2663f160acad270a3831bf7235310c5536163c89114eeb479?s=96&d=mm&r=g\",\"caption\":\"Ramesh Kotha\"},\"description\":\"Ramesh is a certified Java professional working in financial domain, He is an application developer and has hands on practice with Java and related technologies. He is passionate about Spring and other open source technologies.\",\"sameAs\":[\"http:\/\/java2practice.com\/\",\"https:\/\/www.facebook.com\/Kotha.Ramesh\",\"http:\/\/www.linkedin.com\/in\/rameshadayana\",\"https:\/\/x.com\/rameshchary\"],\"url\":\"https:\/\/www.webcodegeeks.com\/author\/ramesh-kotha\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Angular 4: Service example - Web Code Geeks - 2026","description":"Welcome to Angular 4 services Angular services are for building out code that can be shared across multiple components. And these are singletons. services","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/angular-4-service-example\/","og_locale":"en_US","og_type":"article","og_title":"Angular 4: Service example - Web Code Geeks - 2026","og_description":"Welcome to Angular 4 services Angular services are for building out code that can be shared across multiple components. And these are singletons. services","og_url":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/angular-4-service-example\/","og_site_name":"Web Code Geeks","article_publisher":"https:\/\/www.facebook.com\/webcodegeeks","article_author":"https:\/\/www.facebook.com\/Kotha.Ramesh","article_published_time":"2017-11-08T10:15:04+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/angularjs-logo.jpg","type":"image\/jpeg"}],"author":"Ramesh Kotha","twitter_card":"summary_large_image","twitter_creator":"@rameshchary","twitter_site":"@webcodegeeks","twitter_misc":{"Written by":"Ramesh Kotha","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/angular-4-service-example\/#article","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/angular-4-service-example\/"},"author":{"name":"Ramesh Kotha","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/ad5479e29af7e3c3a3295a5ff859221d"},"headline":"Angular 4: Service example","datePublished":"2017-11-08T10:15:04+00:00","mainEntityOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/angular-4-service-example\/"},"wordCount":1462,"commentCount":1,"publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/angular-4-service-example\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/angularjs-logo.jpg","articleSection":["Angular.js"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/angular-4-service-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/angular-4-service-example\/","url":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/angular-4-service-example\/","name":"Angular 4: Service example - Web Code Geeks - 2026","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/angular-4-service-example\/#primaryimage"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/angular-4-service-example\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/angularjs-logo.jpg","datePublished":"2017-11-08T10:15:04+00:00","description":"Welcome to Angular 4 services Angular services are for building out code that can be shared across multiple components. And these are singletons. services","breadcrumb":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/angular-4-service-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/angular-4-service-example\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/angular-4-service-example\/#primaryimage","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/angularjs-logo.jpg","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/angularjs-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/angular-4-service-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.webcodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"JavaScript","item":"https:\/\/www.webcodegeeks.com\/category\/javascript\/"},{"@type":"ListItem","position":3,"name":"Angular.js","item":"https:\/\/www.webcodegeeks.com\/category\/javascript\/angular-js\/"},{"@type":"ListItem","position":4,"name":"Angular 4: Service example"}]},{"@type":"WebSite","@id":"https:\/\/www.webcodegeeks.com\/#website","url":"https:\/\/www.webcodegeeks.com\/","name":"Web Code Geeks","description":"Web Developers Resource Center","publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.webcodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.webcodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.webcodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webcodegeeks","https:\/\/x.com\/webcodegeeks"]},{"@type":"Person","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/ad5479e29af7e3c3a3295a5ff859221d","name":"Ramesh Kotha","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/7fb4cc19e57a57b2663f160acad270a3831bf7235310c5536163c89114eeb479?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/7fb4cc19e57a57b2663f160acad270a3831bf7235310c5536163c89114eeb479?s=96&d=mm&r=g","caption":"Ramesh Kotha"},"description":"Ramesh is a certified Java professional working in financial domain, He is an application developer and has hands on practice with Java and related technologies. He is passionate about Spring and other open source technologies.","sameAs":["http:\/\/java2practice.com\/","https:\/\/www.facebook.com\/Kotha.Ramesh","http:\/\/www.linkedin.com\/in\/rameshadayana","https:\/\/x.com\/rameshchary"],"url":"https:\/\/www.webcodegeeks.com\/author\/ramesh-kotha\/"}]}},"_links":{"self":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/19091","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/users\/1516"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/comments?post=19091"}],"version-history":[{"count":0,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/19091\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media\/909"}],"wp:attachment":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media?parent=19091"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/categories?post=19091"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/tags?post=19091"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}