Skip to content

Commit 209a365

Browse files
santoshyadavdevatscott
authored andcommitted
docs: add explanation for providedIn any (angular#35283)
Angular 9 introduces a new value for providedIn called `any` which lets us use unique instance for servicec in each lazy loaded module, this PR is to document the same fixes angular#35179 PR Close angular#35283
1 parent fb735d6 commit 209a365

File tree

4 files changed

+19
-0
lines changed

4 files changed

+19
-0
lines changed

.pullapprove.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ groups:
366366
'aio/content/images/guide/pipes/**',
367367
'aio/content/guide/providers.md',
368368
'aio/content/examples/providers/**',
369+
'aio/content/images/guide/providers/**',
369370
'aio/content/guide/singleton-services.md',
370371
'aio/content/guide/set-document-title.md',
371372
'aio/content/examples/set-document-title/**',
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Injectable } from '@angular/core';
2+
3+
@Injectable({
4+
providedIn: 'any',
5+
})
6+
export class UserService {
7+
}

aio/content/guide/providers.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ Any component created within a lazy loaded module’s context, such as by router
5252

5353
Though you can provide services by lazy loading modules, not all services can be lazy loaded. For instance, some modules only work in the root module, such as the Router. The Router works with the global location object in the browser.
5454

55+
As of Angular version 9, you can provide a new instance of a service with each lazy loaded module. The following code adds this functionality to `UserService`.
56+
57+
<code-example path="providers/src/app/user.service.2.ts" header="src/app/user.service.ts"></code-example>
58+
59+
With `providedIn: 'any'`, all eagerly loaded modules share a singleton instance; however, lazy loaded modules each get their own unique instance, as shown in the following diagram.
60+
61+
<img src="generated/images/guide/providers/any-provider.svg" alt="any-provider-scope" class="left">
62+
5563

5664
## Limiting provider scope with components
5765

Lines changed: 3 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)