{"id":1952,"date":"2017-04-22T14:54:11","date_gmt":"2017-04-22T14:54:11","guid":{"rendered":"http:\/\/salientsoft.co.uk\/?p=1952"},"modified":"2018-10-10T17:15:57","modified_gmt":"2018-10-10T17:15:57","slug":"declaring-providers-for-dependency-injection","status":"publish","type":"post","link":"https:\/\/salientsoft.co.uk\/?p=1952","title":{"rendered":"Declaring Providers for Dependency Injection"},"content":{"rendered":"<p>Providers need to be declared as they \u2018provide\u2019 the objects to be injected \u2013 in many cases this will be a singleton reused whenever injected (much like a normal Spring bean in Java).<\/p>\n<p>To be visible everywhere in the entire application, they should be declared in the Providers list in the root @NgModule&#160; (e.g. app.module.ts).<\/p>\n<p>However if not used globally, they can be for example registered in a component whereupon they will be visible to that component and its children only.<\/p>\n<p><a href=\"https:\/\/angular.io\/docs\/ts\/latest\/guide\/dependency-injection.html#!#ngmodule-vs-comp\">This angular doc here<\/a> discusses dependency injection and where to register providers.<\/p>\n<p><a href=\"https:\/\/angular.io\/docs\/ts\/latest\/cookbook\/ngmodule-faq.html#!#q-root-component-or-module\">This angular doc here<\/a> is the NgModule faq and also discusses the issue. It points out that registering the providers in the root AppModule (@NgModule as above) is the normal use case, and other cases are rare. In particular it makes the following points:-<\/p>\n<blockquote>\n<h4>Should I add application-wide providers to the root <em>AppModule<\/em> or the root <em>AppComponent<\/em>?<\/h4>\n<p>Register application-wide providers in the root <code>AppModule<\/code>, not in the <code>AppComponent<\/code>.<\/p>\n<p>Lazy-loaded modules and their components can inject <code>AppModule<\/code> services; they can&#8217;t inject <code>AppComponent<\/code> services.<\/p>\n<p>Register a service in <code>AppComponent<\/code> providers <em>only<\/em> if the service must be hidden from components outside the <code>AppComponent<\/code> tree. This is a rare use case.<\/p>\n<p>More generally, <a href=\"https:\/\/angular.io\/docs\/ts\/latest\/cookbook\/ngmodule-faq.html#q-component-or-module\">prefer registering providers in modules<\/a> to registering in components.<\/p>\n<h4><font size=\"3\">Discussion<\/font><\/h4>\n<p>Angular registers all startup module providers with the application root injector. The services created from root injector providers are available to the entire application. They are <em>application-scoped<\/em>.<\/p>\n<p>Certain services (such as the <code>Router<\/code>) only work when registered in the application root injector.<\/p>\n<p>By contrast, Angular registers <code>AppComponent<\/code> providers with the <code>AppComponent<\/code>&#8216;s own injector. <code>AppComponent<\/code> services are available only to that component and its component tree. They are <em>component-scoped<\/em>.<\/p>\n<p>The <code>AppComponent<\/code>&#8216;s injector is a <em>child<\/em> of the root injector, one down in the injector hierarchy. For applications that don&#8217;t use the router, that&#8217;s <em>almost<\/em> the entire application. But for routed applications, &quot;almost&quot; isn&#8217;t good enough.<\/p>\n<p><code>AppComponent<\/code> services don&#8217;t exist at the root level where routing operates. Lazy-loaded modules can&#8217;t reach them. In the NgModule page sample applications, if you had registered <code>UserService<\/code> in the <code>AppComponent<\/code>, the <code>HeroComponent<\/code> couldn&#8217;t inject it. The application would fail the moment a user navigated to &quot;Heroes&quot;.<\/p>\n<p><a><\/a><\/p>\n<h4>Should I add other providers to a module or a component?<\/h4>\n<p>In general, prefer registering feature-specific providers in modules (<code>@NgModule.providers<\/code>) to registering in components (<code>@Component.providers<\/code>).<\/p>\n<p>Register a provider with a component when you <em>must<\/em> limit the scope of a service instance to that component and its component tree. Apply the same reasoning to registering a provider with a directive.<\/p>\n<p>For example, a hero editing component that needs a private copy of a caching hero service should register the <code>HeroService<\/code> with the <code>HeroEditorComponent<\/code>. Then each new instance of the <code>HeroEditorComponent<\/code> gets its own cached service instance. The changes that editor makes to heroes in its service don&#8217;t touch the hero instances elsewhere in the application.<\/p>\n<p><a href=\"https:\/\/angular.io\/docs\/ts\/latest\/cookbook\/ngmodule-faq.html#q-root-component-or-module\">Always register <em>application-wide<\/em> services with the root <code>AppModule<\/code><\/a>, not the root <code>AppComponent<\/code>.<\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Providers need to be declared as they \u2018provide\u2019 the objects to be injected \u2013 in many cases this will be a singleton reused whenever injected (much like a normal Spring bean in Java). To be visible everywhere in the entire application, they should be declared in the Providers list in the root @NgModule&#160; (e.g. app.module.ts). [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[198],"tags":[199,16],"_links":{"self":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/1952"}],"collection":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1952"}],"version-history":[{"count":3,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/1952\/revisions"}],"predecessor-version":[{"id":1955,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/1952\/revisions\/1955"}],"wp:attachment":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1952"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1952"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1952"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}