Angular service registered with container without specifying "providedIn"

Viewed 51

The angular docs recommend registering a service with the container 1) in the root scope, generally, or 2) in the scope of a particular module.

So in the general case, like this:

@Injectable({ providedIn: "root" })

However I see lots of code like this:

@Injectable()

In that case, in which scope will the service be registered? What are the implications of not specifying a scope, and how does that affect tree shaking?

1 Answers

In case of @Injectable(),it is registered in component level or module level in providers array

Component level:

@Component({
    selector: 'my-app',
    template: '<h1>{{ title }}</h1>',
    providers: [ExampleService]
})

Module Level:

@NgModule({
  //other metadata properties
  providers: [ExampleService]
})
Related