<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Saurabha Arya</title>
    <description>The latest articles on DEV Community by Saurabha Arya (@arya_saurabha_0501).</description>
    <link>https://dev.to/arya_saurabha_0501</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3618893%2F38617456-dbcc-4bdb-a3bb-56739fb9c401.jpg</url>
      <title>DEV Community: Saurabha Arya</title>
      <link>https://dev.to/arya_saurabha_0501</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/arya_saurabha_0501"/>
    <language>en</language>
    <item>
      <title>Angular Standalone Migration: A Deep Dive into Dependency Injection Pitfalls &amp; Best Practices</title>
      <dc:creator>Saurabha Arya</dc:creator>
      <pubDate>Wed, 21 Jan 2026 06:01:17 +0000</pubDate>
      <link>https://dev.to/arya_saurabha_0501/angular-standalone-migration-a-deep-dive-into-dependency-injection-pitfalls-best-practices-17l1</link>
      <guid>https://dev.to/arya_saurabha_0501/angular-standalone-migration-a-deep-dive-into-dependency-injection-pitfalls-best-practices-17l1</guid>
      <description>&lt;p&gt;&lt;strong&gt;🚀 Upgrading a large Angular application from v17 to v21 isn’t just a version bump — it was an architectural wake-up call.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;One of the biggest shifts I had to tackle was moving the app to a &lt;strong&gt;fully standalone architecture&lt;/strong&gt;. That change alone forced me to pause and ask an important question:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;👉 “Do we actually understand how dependency injection is working in our app?”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;As I dug deeper, I realized many DI patterns in the codebase worked by coincidence, not by design. They had survived for years thanks to &lt;code&gt;NgModules&lt;/code&gt;— but in a standalone world, they suddenly felt fragile, confusing, or outright wrong.&lt;/p&gt;

&lt;p&gt;🔍 In this article, I’ll walk through &lt;strong&gt;real cases from a production app&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;what the original code looked like&lt;/li&gt;
&lt;li&gt;why it worked earlier&lt;/li&gt;
&lt;li&gt;where it breaks or misleads in standalone Angular&lt;/li&gt;
&lt;li&gt;and how to fix it &lt;strong&gt;cleanly and correctly&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you’re upgrading an existing Angular app or refactoring legacy DI patterns — this guide is for you.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;⚠️ &lt;em&gt;The migration is still ongoing — I’ll be publishing more articles as I continue the upgrade.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  1. From &lt;code&gt;NgModule.providers&lt;/code&gt; to &lt;code&gt;providedIn: 'root'&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;❌ Legacy pattern (NgModule era)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxajiwh2vq3grxbg9drrd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxajiwh2vq3grxbg9drrd.png" alt=" " width="800" height="289"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This worked because:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The module owned the DI scope&lt;/li&gt;
&lt;li&gt;Services were implicitly singleton within the module&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;✅ Standalone replacement&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft1whwybr7ujebl31wwil.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft1whwybr7ujebl31wwil.png" alt=" " width="800" height="323"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why this is correct&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;providedIn: 'root'&lt;/code&gt; replaces module-level providers&lt;/li&gt;
&lt;li&gt;Singleton behavior is preserved&lt;/li&gt;
&lt;li&gt;Tree-shakable and future-proof&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Rule:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If a service was previously in &lt;code&gt;Module.providers&lt;/code&gt;, it should almost always move to &lt;code&gt;providedIn: 'root'&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  2. Services in &lt;code&gt;bootstrapApplication&lt;/code&gt; — what really belongs there?
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;❌ What I initially had in &lt;code&gt;main.ts&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fawprl37dpcrmigq5y65x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fawprl37dpcrmigq5y65x.png" alt=" " width="800" height="424"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This works, but it’s not a correct standalone design.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;✅ What SHOULD be in &lt;code&gt;main.ts&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnms5fnj1qk4dge7wi6lw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnms5fnj1qk4dge7wi6lw.png" alt=" " width="800" height="926"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What &lt;code&gt;bootstrapApplication&lt;/code&gt; is for:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;✅ Framework overrides (&lt;code&gt;ErrorHandler&lt;/code&gt;, &lt;code&gt;TitleStrategy&lt;/code&gt;, etc.)&lt;br&gt;
✅ Router setup &amp;amp; strategies&lt;br&gt;
✅ HttpClient + interceptors&lt;br&gt;
✅ App-wide configuration tokens&lt;br&gt;
✅ Platform / framework modules&lt;br&gt;
✅ Legacy services you cannot modify&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What it is NOT for:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;❌ API / CRUD services&lt;br&gt;
❌ Business/domain services&lt;br&gt;
❌ Feature logic&lt;br&gt;
❌ Component state services&lt;/p&gt;




&lt;h3&gt;
  
  
  3. Services with &lt;code&gt;Subject&lt;/code&gt; / &lt;code&gt;BehaviorSubject&lt;/code&gt;: Root or not?
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;My use case&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;App loads a master API&lt;/li&gt;
&lt;li&gt;Stores success/error messages in a BehaviorSubject&lt;/li&gt;
&lt;li&gt;Multiple components read from it&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;❌ Legacy misuse&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F195u2b7zqhahlquz8i9u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F195u2b7zqhahlquz8i9u.png" alt=" " width="800" height="255"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This creates:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Multiple instances&lt;/li&gt;
&lt;li&gt;Multiple subscriptions&lt;/li&gt;
&lt;li&gt;Hidden bugs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;✅ Correct design&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7m5w0kbp73h318qfyfoj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7m5w0kbp73h318qfyfoj.png" alt=" " width="800" height="257"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If a service stores state that many parts of the app use, it should live at the root so everyone gets the same data.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  4. Services in component &lt;code&gt;providers&lt;/code&gt;+ &lt;code&gt;providedIn: 'root'&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Yes, Angular allows this — but it’s dangerous.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flfeaekug0r17pbmg8bek.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flfeaekug0r17pbmg8bek.png" alt=" " width="800" height="221"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4b7557wlalyta72cbamk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4b7557wlalyta72cbamk.png" alt=" " width="800" height="255"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Result&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Component gets a new instance&lt;/li&gt;
&lt;li&gt;App gets a different instance&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;⚠️ For API, auth, socket, or state services — this is almost always a bug.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  5. Pipes (&lt;code&gt;DatePipe&lt;/code&gt;&amp;amp; custom pipes) in &lt;code&gt;providers&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;❌ What I found&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwvzl2xmtagatgfuof7l6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwvzl2xmtagatgfuof7l6.png" alt=" " width="800" height="255"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;✅ Correct usage&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9h3ylmf196kxkowiey41.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9h3ylmf196kxkowiey41.png" alt=" " width="800" height="255"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Or simply inject without providing:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu9udcz3aklg6dwmiv2t9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu9udcz3aklg6dwmiv2t9.png" alt=" " width="800" height="203"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Custom pipes used only in &lt;code&gt;.ts&lt;/code&gt;?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;👉 They should not be pipes — convert them to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;utility functions&lt;/li&gt;
&lt;li&gt;or services&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  6. Components injected into other components (⚠️ critical)
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;❌ Legacy anti-pattern I found&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq4knju9r10z27z5b5nl9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq4knju9r10z27z5b5nl9.png" alt=" " width="800" height="383"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This creates a &lt;strong&gt;ghost component instance&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Not rendered&lt;/li&gt;
&lt;li&gt;Not part of the DOM&lt;/li&gt;
&lt;li&gt;Not change-detected&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;✅ Correct approaches&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Option 1: &lt;code&gt;@ViewChild&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1x3d6bjd4yj4mkd50rts.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1x3d6bjd4yj4mkd50rts.png" alt=" " width="800" height="255"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Option 2: Shared service (recommended)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1gqy6pphg0dhhnnpuxto.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1gqy6pphg0dhhnnpuxto.png" alt=" " width="800" height="323"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Components should NEVER appear in providers.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  7. &lt;code&gt;.spec.ts&lt;/code&gt; files and providers — is that okay?
&lt;/h3&gt;

&lt;p&gt;Yes — &lt;strong&gt;tests have their own DI world&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fznhvxfu483c51i88c389.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fznhvxfu483c51i88c389.png" alt=" " width="800" height="255"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Is test-scoped&lt;/li&gt;
&lt;li&gt;Does not affect runtime DI&lt;/li&gt;
&lt;li&gt;Is perfectly valid&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;No changes needed during migration.&lt;/p&gt;




&lt;h3&gt;
  
  
  8. GlobalErrorHandler — the exception that belongs in &lt;code&gt;main.ts&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbw3ictsk83rcilkau4o0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbw3ictsk83rcilkau4o0.png" alt=" " width="800" height="278"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fklzswtgg30s18ygzdbpy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fklzswtgg30s18ygzdbpy.png" alt=" " width="800" height="371"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why it must stay in &lt;code&gt;main.ts&lt;/code&gt; and not part of a component?&lt;/strong&gt;&lt;br&gt;
🧠 Angular itself uses &lt;code&gt;ErrorHandler&lt;/code&gt;, so it must be registered during app bootstrap, before any component is created.&lt;br&gt;
🌍 Error handling must be truly global — errors can occur outside component lifecycles and feature scopes.&lt;br&gt;
🏗️ Components manage UI, not framework behavior, and global error handling is part of Angular’s core infrastructure.&lt;br&gt;
⚠️ Providing it in components can create multiple instances, leading to inconsistent or missed error handling.&lt;/p&gt;




&lt;h2&gt;
  
  
  Final Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Standalone Angular removes NgModules, not DI rules&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;providedIn: 'root'&lt;/code&gt; replaces most module-level providers&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;bootstrapApplication&lt;/code&gt; is for framework wiring only&lt;/li&gt;
&lt;li&gt;Components must never be used as services&lt;/li&gt;
&lt;li&gt;Stateful services belong at root unless isolation is intentional&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🚀 If you’re migrating to standalone Angular or have run into DI-related challenges, feel free to share your questions or experiences in the comments — I’d love to discuss them 🙂&lt;/p&gt;

</description>
      <category>angular</category>
      <category>typescript</category>
      <category>webdev</category>
      <category>frontend</category>
    </item>
  </channel>
</rss>
