{"id":3733,"date":"2020-05-14T19:23:32","date_gmt":"2020-05-14T19:23:32","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=3733"},"modified":"2021-06-17T14:05:44","modified_gmt":"2021-06-17T14:05:44","slug":"services-basics","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/services-basics\/","title":{"rendered":"Services Basics"},"content":{"rendered":"<ol>\n<li>Services are mostly used in displaying datas from APIs<\/li>\n<li>To generate a new service use <em>ng generate service Services\/SERVICE_NAME<\/em> <\/li>\n<li>services are injectable because they would be mostly called by other components<\/li>\n<li>Injection of services can happen at three level\n<ul>\n<li><strong>AppModule <\/strong>\t \t\t&#8211; Same Instance of Service Injected would be available across application<\/li>\n<li><strong>AppComponent \t\t<\/strong>&#8211; Same Instance of Service Injected at this level would be available in this component and all child component<\/li>\n<li><strong>Any Other Component<\/strong> &#8211; Same Instance of Service Injected would be available to this component and child component(not to parent component)<\/li>\n<\/ul>\n<\/li>\n<li>@Injectable is not needed if the Service is added in app.modules.ts<\/li>\n<\/ol>\n<p><strong>aboutus.component.ts<\/strong><\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n.\r\n.\r\nconstructor(private objApi:ApiService) { \r\n    this.objApi.getDataFromRest();\r\n  }\r\n.\r\n.\r\n<\/pre>\n<p><strong>api.service.ts<\/strong><\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nimport { Injectable } from '@angular\/core';\r\n\r\n@Injectable({\r\n  providedIn: 'root'\r\n})\r\nexport class ApiService {\r\n\r\n  constructor() { }\r\n\r\n  getDataFromRest()\r\n  {\r\n    return console.log(&quot;Data from Rest Method&quot;);\r\n  }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Services are mostly used in displaying datas from APIs To generate a new service use ng generate service Services\/SERVICE_NAME services are injectable because they would be mostly called by other components Injection of services can happen at three level AppModule &#8211; Same Instance of Service Injected would be available across application AppComponent &#8211; Same Instance&hellip; <a href=\"https:\/\/codethataint.com\/blog\/services-basics\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[301],"tags":[302],"class_list":["post-3733","post","type-post","status-publish","format-standard","hentry","category-services","tag-services"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3733","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=3733"}],"version-history":[{"count":5,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3733\/revisions"}],"predecessor-version":[{"id":4375,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3733\/revisions\/4375"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=3733"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=3733"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=3733"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}