Angular Singleton Services

Angular Singleton Services

Angular singleton services is an essential part of AngularJS. So what do I mean by singleton service in the Angular world? Here I am talking about a service instance that is shared across components. Think one service with a given state shared across any component that imports and references the service in its constructor.

An injector created from a module definition will have services which are singletons with respect to that injector. To control the lifetime of services, one controls the creation and destruction of injectors. For example, a route will have an associated module. When the route is activated, an injector is created from that module as a child of the current injector. When you navigate away from the route, the injector is destroyed. This means that services declared in a routing module will have a lifetime equal to that of the route. Similarly, services provided in an application module will have the same lifetime of the application, hence singleton.

The following example module is called, as a convention, CoreModule. This use of @NgModule creates organizational infrastructure and gives you a way of providing services from a designated NgModule.

 

IT Professionals, Web Developers, Web Programmers, IT students can Apply for the certification course to move ahead in their careers.

Angular 4 Tutorial IndexBack to Angular 4 Tutorial Main Page

Share this post
[social_warfare]
Providers in Angular
Angular Lazy Loading Feature Modules

Get industry recognized certification – Contact us

keyboard_arrow_up