{"id":646,"date":"2023-03-13T05:00:00","date_gmt":"2023-03-13T12:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=646"},"modified":"2023-03-09T16:02:40","modified_gmt":"2023-03-10T00:02:40","slug":"dependency-injection-and-provider-config","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/dependency-injection-and-provider-config\/","title":{"rendered":"Dependency Injection and Provider Config"},"content":{"rendered":"\n<p>As Angular developers, we use and create services fairly often. So, in today&#8217;s post, let&#8217;s demystify the following syntax and see what the different options for it are:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"293\" height=\"126\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/03\/image-15.png\" alt=\"\" class=\"wp-image-647\"\/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>When a service is <strong>provided in the root injector<\/strong> (<code>providedIn: 'root'<\/code>), it means that such a service can be injected into any component of your application, and that service will be a singleton shared by all consumers in your application.<\/p>\n\n\n\n<p>If we want to restrict the scope of a service, we can provide it in a <strong>module <\/strong>or a <strong>component <\/strong>instead of the root injector. The syntax looks like this &#8211; we use the class name of the targeted module or component:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"314\" height=\"117\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/03\/image-17.png\" alt=\"\" class=\"wp-image-650\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/03\/image-17.png 314w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/03\/image-17-300x112.png 300w\" sizes=\"auto, (max-width: 314px) 100vw, 314px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>When a service is provided in a module, only the components\/directives\/pipes\/services of that module can inject that service. However, when provided in a component, that component and <strong>its children<\/strong> can inject that service.<\/p>\n\n\n\n<p>From a syntax standpoint, it&#8217;s also important to mention that using <code>providedIn<\/code> is equivalent to doing the following in a component or a module:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"392\" height=\"136\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/03\/image-18.png\" alt=\"\" class=\"wp-image-651\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/03\/image-18.png 392w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/03\/image-18-300x104.png 300w\" sizes=\"auto, (max-width: 392px) 100vw, 392px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>The above code is equivalent to the following:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"294\" height=\"116\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/03\/image-19.png\" alt=\"\" class=\"wp-image-652\"\/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>So you only need one or the other; both would be redundant.<\/p>\n\n\n\n<p>Finally, there is another option available: <code>platform<\/code>.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"290\" height=\"117\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/03\/image-20.png\" alt=\"\" class=\"wp-image-653\"\/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>The service would be injectable into any Angular app in the current browser tab. It&#8217;s a rare use case, but if you&#8217;re running multiple Angular applications on the same page and want to share a service between these applications, <code>platform<\/code> is what you need.<\/p>\n\n\n\n<p>For more information, you can read <a href=\"https:\/\/blog.angulartraining.com\/dependency-injection-with-angular-9-63ce524496d9\" target=\"_blank\" rel=\"noopener\" title=\"\">that post of mine that covers all these options<\/a> (note: <code>providedIn: 'any'<\/code> used to be an option but is now deprecated, which is why I didn&#8217;t mention it)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As Angular developers, we use and create services fairly often. So, in today&#8217;s post, let&#8217;s demystify the following syntax and see what the different options for it are: When a service is provided in the root injector (providedIn: &#8216;root&#8217;), it means that such a service can be injected into any component of your application, and [&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-646","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\/646","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=646"}],"version-history":[{"count":3,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/646\/revisions"}],"predecessor-version":[{"id":655,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/646\/revisions\/655"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=646"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=646"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=646"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}