What is the purpose of providedIn in the Injectable decorator when creating services in Angular 6?
Hello everyone! I started learning Angular 6 and TypeScript, and when generating a service via CLI I noticed this thing:
```typescript
@Injectable({
providedIn: 'root'
})
export class MyService {
constructor() { }
}
```
Previously, in Angular 5, I just wrote `@Injectable()` without any parameters, and then registered the service in the module in the `providers` section. And now CLI generates this `providedIn: 'root'`. I'm a bit confused.
Here's how I used to do it:
```typescript
// my.service.ts
@Injectable()
export class MyService { }
// app.module.ts
@NgModule({
providers: [MyService]
})
export class AppModule { }
```
And now the new approach:
```typescript
// my.service.ts
@Injectable({
providedIn: 'root'
})
export class MyService { }
```
Questions:
1. What exactly does
No answers yet.
Log in to leave an answer
Blogs
Caching in C#: Redis, Memcached, and Built-in Solutions from A to Z
Asynchronous Call from Synchronous Code in C#: Battle of Approaches
Deploying C# on Kubernetes: A Basic Practical Guide
Smooth scroll to element with jQuery: 5 fatal mistakes