{"id":656,"date":"2023-03-14T05:00:00","date_gmt":"2023-03-14T12:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=656"},"modified":"2024-10-21T12:52:23","modified_gmt":"2024-10-21T19:52:23","slug":"dependency-injection-priority-and-hierarchy","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/dependency-injection-priority-and-hierarchy\/","title":{"rendered":"Dependency Injection Priority and Hierarchy"},"content":{"rendered":"\n<p>Yesterday, we covered the <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/dependency-injection-and-provider-config\/\" target=\"_blank\" rel=\"noopener\" title=\"\">different options to configure our providers <\/a>with the <code>providedIn<\/code> syntax. Today, let&#8217;s look at how Angular resolves dependency injection using <a href=\"https:\/\/angular.dev\/guide\/di\/hierarchical-dependency-injection\" target=\"_blank\" rel=\"noopener\" title=\"\">hierarchical injectors<\/a>.<\/p>\n\n\n\n<p>Let&#8217;s assume we have a <code>ButtonComponent<\/code> that needs a <code>LoginService<\/code>. Angular is going to check the injector of <code>ButtonComponent<\/code> to see if a <code>LoginService<\/code> is available there (since all components have their own injector, configurable with the array of <code>providers<\/code> or the <code>providedIn<\/code> syntax). <\/p>\n\n\n\n<p>Suppose the component injector doesn&#8217;t know about that service. In that case, it&#8217;s going to ask its parent component, then grandparent, making its way up the component hierarchy till it gets to the root injector of the application:<\/p>\n\n\n\n<p class=\"has-text-align-center\"><img decoding=\"async\" width=\"614px;\" height=\"345px;\" src=\"https:\/\/lh6.googleusercontent.com\/hMywROWtfqz3RSnwAAHYezJMXHfHKsIZvlUSJ8g2e-uzMWNu22ZukmmAzJ-BOIrQXLUDw94yu7kAJmpSGmySUmhLLa0Mse1cBdgvDwJ9c7vH0mGOuJZspXWmIp-jopFeN_veUmwGLoQCZtK7yoRcUr_ThniIdjCU9gDu4fLsE0mGw1H5iBZou3ABz03XRdzi=s2048\"><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>These are called <strong>hierarchical injectors<\/strong>: They rely on our component hierarchy. This explains why changing the providers&#8217; config at the component level impacts a component and all its children.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What about modules?<\/h2>\n\n\n\n<p>If a service cannot be found in the component tree, then Angular is going to use a similar resolution path using the <strong>module hierarchy<\/strong>, from the child modules all the way up to the <code>AppModule<\/code>.<\/p>\n\n\n\n<p>If no service is found, then Angular throws an error stating no provider is available for that service.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Yesterday, we covered the different options to configure our providers with the providedIn syntax. Today, let&#8217;s look at how Angular resolves dependency injection using hierarchical injectors. Let&#8217;s assume we have a ButtonComponent that needs a LoginService. Angular is going to check the injector of ButtonComponent to see if a LoginService is available there (since all [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,26],"tags":[],"class_list":["post-656","post","type-post","status-publish","format-standard","hentry","category-angular","category-dependency-injection"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/656","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/comments?post=656"}],"version-history":[{"count":4,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/656\/revisions"}],"predecessor-version":[{"id":2210,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/656\/revisions\/2210"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=656"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=656"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=656"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}