Skip to content

Commit d9ac538

Browse files
authored
a(module): add providedIn use case
1 parent 96940b7 commit d9ac538

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

ngModule.md

+18
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
To avoid having multiple instances of services, RouterModule defines two methods, "forRoot" and "forChild". As the name suggests, "forRoot" method should be called only by root module, i.e. app.module, and forChild should be called by other feature modules. This way, you still get to use directives, components, pipes exported by this module and don't get new instances of services.
2222
If you want to define such module yourself, you can do it as following
2323

24+
25+
* What is providedIn property used for in an NgModule?
26+
27+
ProvidedIn is used to specify that a service should be provided in a particular @NgModule. For example, if you don't want UserService to be available to applications unless they import a UserModule you've created, you can specify that the service should be provided in the module:
2428
```ts
2529
@NgModule({
2630
declarations: [MyAwesomeComponent, MyCoolDirective],
@@ -35,6 +39,20 @@
3539
}
3640
}
3741
```
42+
43+
[Read more:](https://angular.io/guide/providers#providedin-and-ngmodules)
44+
45+
```ts
46+
import { Injectable } from '@angular/core';
47+
import { UserModule } from './user.module';
48+
49+
@Injectable({
50+
providedIn: UserModule,
51+
})
52+
export class UserService {
53+
}f
54+
```
55+
3856
* What would you have in a shared module?
3957
I would put directives, pipes, components and other modules that are used throughout my application and export them from this shared module.
4058
This way, I would not have to declare/import same components/modules everywhere.

0 commit comments

Comments
 (0)