{"id":45,"date":"2026-07-04T09:30:05","date_gmt":"2026-07-04T09:30:05","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/angular\/05-angular-services-di\/"},"modified":"2026-07-04T09:42:49","modified_gmt":"2026-07-04T09:42:49","slug":"05-angular-services-di","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/angular\/05-angular-services-di\/","title":{"rendered":"Angular Services and Dependency Injection (Angular 21+)"},"content":{"rendered":"<p><!-- ktg-updated-banner --><\/p>\n<div class=\"ktg-updated-banner\" style=\"margin:0 0 1.25em;padding:0.75em 1em;background:#fefce8;border-left:4px solid #ca8a04;border-radius:4px;\">\n<p><strong>Updated July 4, 2026.<\/strong> Refreshed for Angular 21 and current SEO best practices.<\/p>\n<\/div>\n<p><!-- ktg-series-toc --><\/p>\n<nav class=\"ktg-series-toc\" aria-label=\"Angular tutorial table of contents\" style=\"margin:1.5em 0;padding:1em 1.25em;background:#f8fafc;border:1px solid #e2e8f0;border-radius:8px;\">\n<h2>Table of Contents<\/h2>\n<p>Follow the Angular tutorial series in order:<\/p>\n<ol>\n<li><a href=\"https:\/\/www.kindsonthegenius.com\/angular\/01-angular-introduction\/\">Lesson 1: Angular Tutorial \u2014 Introduction (2026)<\/a><\/li>\n<li><a href=\"https:\/\/www.kindsonthegenius.com\/angular\/02-angular-cli-setup\/\">Lesson 2: Angular CLI Setup \u2014 Create Your First App (Angular 21+)<\/a><\/li>\n<li><a href=\"https:\/\/www.kindsonthegenius.com\/angular\/03-angular-components-and-templates\/\">Lesson 3: Angular Components and Templates \u2014 Build Your First UI<\/a><\/li>\n<li><a href=\"https:\/\/www.kindsonthegenius.com\/angular\/04-angular-routing\/\">Lesson 4: Angular Routing \u2014 Navigate Between Pages (Angular 21+)<\/a><\/li>\n<li><strong aria-current=\"page\">Lesson 5: Angular Services and Dependency Injection (Angular 21+)<\/strong><\/li>\n<li><a href=\"https:\/\/www.kindsonthegenius.com\/angular\/06-angular-http-client\/\">Lesson 6: Angular HTTP Client \u2014 Fetch Data from APIs (Angular 21+)<\/a><\/li>\n<li><a href=\"https:\/\/www.kindsonthegenius.com\/angular\/07-angular-forms\/\">Lesson 7: Angular Forms \u2014 Reactive Forms and Signal Forms Preview (Angular 21+)<\/a><\/li>\n<li><a href=\"https:\/\/www.kindsonthegenius.com\/angular\/\">\u2190 Back to Angular Tutorial hub<\/a><\/li>\n<\/ol>\n<\/nav>\n<p><!-- ktg-angular-modern --><\/p>\n<div class=\"ktg-angular-modern\" style=\"margin:1.5em 0;padding:1em;background:#eff6ff;border-left:4px solid #2563eb;border-radius:4px;\">\n<h4>Angular 21 Notes<\/h4>\n<p>Use <code>@Injectable({ providedIn: 'root' })<\/code> and <code>inject()<\/code> in standalone components \u2014 the modern replacement for constructor injection boilerplate.<\/p>\n<\/div>\n<p>This lesson explains <strong>Angular services<\/strong> and <strong>dependency injection (DI)<\/strong> \u2014 how to share logic, API clients, and state across components without prop drilling. Angular&#8217;s DI container is a core reason the framework scales in enterprise codebases.<\/p>\n<p><strong>Prerequisites:<\/strong> Lessons 1\u20134. <strong>Estimated time:<\/strong> 40\u201350 minutes.<\/p>\n<h4 id=\"what-is-service\">1. What Is a Service?<\/h4>\n<p>A service is a TypeScript class responsible for a focused capability \u2014 fetching users, wrapping localStorage, or encapsulating business rules. Components stay thin; services hold reusable logic.<\/p>\n<pre><code class=\"language-bash\">ng generate service services\/greeting --skip-tests\n<\/code><\/pre>\n<figure style=\"margin:1.5em 0;text-align:center;\"><img decoding=\"async\" src=\"https:\/\/www.kindsonthegenius.com\/angular\/wp-content\/uploads\/2026\/07\/05-angular-services-di-section-1.jpg\" alt=\"Angular service layer architecture diagram\" width=\"1200\" height=\"675\" loading=\"lazy\" style=\"max-width:100%;height:auto;border-radius:8px;border:1px solid #dee2e6;\" \/><figcaption style=\"font-size:0.85em;color:#666;margin-top:0.5em;\">Photo by <a href=\"https:\/\/unsplash.com\/@jakubzerdzicki?utm_source=kindsonthegenius&#038;utm_medium=referral\" rel=\"noopener noreferrer\" target=\"_blank\">Jakub \u017berdzicki<\/a> on <a href=\"https:\/\/unsplash.com\/?utm_source=kindsonthegenius&#038;utm_medium=referral\" rel=\"noopener noreferrer\" target=\"_blank\">Unsplash<\/a><\/figcaption><\/figure>\n<h4 id=\"injectable\">2. @Injectable and providedIn: &#8216;root&#8217;<\/h4>\n<pre><code class=\"language-typescript\">import { Injectable, signal } from '@angular\/core';\n\n@Injectable({ providedIn: 'root' })\nexport class GreetingService {\n  private readonly messages = signal&lt;string[]&gt;([]);\n\n  add(message: string) {\n    this.messages.update((list) =&gt; [...list, message]);\n  }\n\n  all() {\n    return this.messages.asReadonly();\n  }\n}\n<\/code><\/pre>\n<p><code>providedIn: 'root'<\/code> registers a singleton for the entire app \u2014 no NgModule providers array required.<\/p>\n<figure style=\"margin:1.5em 0;text-align:center;\"><img decoding=\"async\" src=\"https:\/\/www.kindsonthegenius.com\/angular\/wp-content\/uploads\/2026\/07\/05-angular-services-di-section-2.jpg\" alt=\"TypeScript injectable service in an Angular project\" width=\"1200\" height=\"675\" loading=\"lazy\" style=\"max-width:100%;height:auto;border-radius:8px;border:1px solid #dee2e6;\" \/><figcaption style=\"font-size:0.85em;color:#666;margin-top:0.5em;\">Photo by <a href=\"https:\/\/unsplash.com\/@cdr6934?utm_source=kindsonthegenius&#038;utm_medium=referral\" rel=\"noopener noreferrer\" target=\"_blank\">Chris Ried<\/a> on <a href=\"https:\/\/unsplash.com\/?utm_source=kindsonthegenius&#038;utm_medium=referral\" rel=\"noopener noreferrer\" target=\"_blank\">Unsplash<\/a><\/figcaption><\/figure>\n<h4 id=\"inject-api\">3. Inject Services with inject()<\/h4>\n<pre><code class=\"language-typescript\">import { Component, inject } from '@angular\/core';\nimport { GreetingService } from '..\/services\/greeting';\n\n@Component({\n  selector: 'app-greeting-list',\n  standalone: true,\n  template: `\n    &lt;ul&gt;\n      @for (msg of service.all()(); track msg) {\n        &lt;li&gt;{{ msg }}&lt;\/li&gt;\n      }\n    &lt;\/ul&gt;\n    &lt;button (click)=\"add()\"&gt;Add&lt;\/button&gt;\n  `,\n})\nexport class GreetingList {\n  protected readonly service = inject(GreetingService);\n\n  add() {\n    this.service.add('Hello from DI');\n  }\n}\n<\/code><\/pre>\n<h4 id=\"testing\">4. Testing Services<\/h4>\n<p>With Vitest (Angular 21 default), configure <code>TestBed<\/code> or instantiate services directly when they have no Angular dependencies:<\/p>\n<pre><code class=\"language-typescript\">const service = new GreetingService();\nservice.add('test');\nexpect(service.all()()).toContain('test');\n<\/code><\/pre>\n<h4 id=\"next\">5. Next Steps<\/h4>\n<p>Continue to <a href=\"https:\/\/www.kindsonthegenius.com\/angular\/06-angular-http-client\/\">Lesson 6 \u2014 HTTP Client<\/a> to load data from REST APIs inside services.<\/p>\n<h4 id=\"faq\">Frequently Asked Questions<\/h4>\n<p><strong>What is dependency injection in Angular?<\/strong><br \/>\nA pattern where Angular creates and supplies class dependencies automatically \u2014 you declare what you need via constructor or <code>inject()<\/code>.<\/p>\n<p><strong>When should I use a service vs a signal in a component?<\/strong><br \/>\nKeep UI-only state in the component. Move shared or API-backed logic into services.<\/p>\n<p><!-- ktg-faq-schema --><br \/>\n<script type=\"application\/ld+json\">{\"@context\":\"https:\/\/schema.org\",\"@type\":\"FAQPage\",\"mainEntity\":[{\"@type\":\"Question\",\"name\":\"What is dependency injection in Angular?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Angular creates and supplies class dependencies via inject() or constructor injection.\"}},{\"@type\":\"Question\",\"name\":\"When should I use a service vs a signal in a component?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Use services for shared or API-backed logic; keep UI-only state in the component.\"}}]}<\/script><\/p>\n<p><!-- ktg-lesson-nav --><\/p>\n<nav class=\"ktg-lesson-nav\" aria-label=\"Lesson navigation\">\n<p><strong>Previous:<\/strong> <a href=\"https:\/\/www.kindsonthegenius.com\/angular\/04-angular-routing\/\">Lesson 4: Angular Routing \u2014 Navigate Between Pages (Angular 21+)<\/a><\/p>\n<p><strong>Next:<\/strong> <a href=\"https:\/\/www.kindsonthegenius.com\/angular\/06-angular-http-client\/\">Lesson 6: Angular HTTP Client \u2014 Fetch Data from APIs (Angular 21+)<\/a><\/p>\n<\/nav>\n<p><!-- ktg-alkademy-cta --><\/p>\n<div class=\"ktg-alkademy-cta\">\n<p><strong>Want live Angular or frontend classes?<\/strong> Join <a href=\"https:\/\/www.alkademy.com\/courses\" target=\"_blank\" rel=\"noopener noreferrer\">Alkademy<\/a> for instructor-led Angular and frontend courses with hands-on projects.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Updated July 4, 2026. Refreshed for Angular 21 and current SEO best practices. Table of Contents Follow the Angular tutorial series in order: Lesson 1: Angular Tutorial \u2014 Introduction (2026) Lesson 2: Angular CLI Setup \u2014 Create Your First App (Angular 21+) Lesson 3: Angular Components and Templates \u2014 Build Your First UI Lesson 4: &#8230; <a title=\"Angular Services and Dependency Injection (Angular 21+)\" class=\"read-more\" href=\"https:\/\/www.kindsonthegenius.com\/angular\/05-angular-services-di\/\" aria-label=\"Read more about Angular Services and Dependency Injection (Angular 21+)\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":42,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[2],"tags":[],"class_list":["post-45","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-angular-tutorials"],"_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/angular\/wp-json\/wp\/v2\/posts\/45","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kindsonthegenius.com\/angular\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kindsonthegenius.com\/angular\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/angular\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/angular\/wp-json\/wp\/v2\/comments?post=45"}],"version-history":[{"count":3,"href":"https:\/\/www.kindsonthegenius.com\/angular\/wp-json\/wp\/v2\/posts\/45\/revisions"}],"predecessor-version":[{"id":70,"href":"https:\/\/www.kindsonthegenius.com\/angular\/wp-json\/wp\/v2\/posts\/45\/revisions\/70"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/angular\/wp-json\/wp\/v2\/media\/42"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/angular\/wp-json\/wp\/v2\/media?parent=45"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/angular\/wp-json\/wp\/v2\/categories?post=45"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/angular\/wp-json\/wp\/v2\/tags?post=45"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}