{"id":1271,"date":"2023-08-02T05:00:00","date_gmt":"2023-08-02T12:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=1271"},"modified":"2023-07-31T12:17:30","modified_gmt":"2023-07-31T19:17:30","slug":"how-to-run-code-in-an-injection-context","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/how-to-run-code-in-an-injection-context\/","title":{"rendered":"How to run code in an injection context?"},"content":{"rendered":"\n<p>Yesterday, I introduced an injection context, and we saw a few locations where we could inject dependencies. This can be limiting in scenarios where we call a method that creates and returns an Observable outside of an injection context, as we can&#8217;t always initialize observables in a constructor. Does that mean we cannot use the <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/angular-16-preview-takeuntildestroyed\/\" target=\"_blank\" rel=\"noopener\" title=\"\"><code>takeUntilDestroyed<\/code> operator<\/a> in those scenarios?<\/p>\n\n\n\n<p>No, because we can store our injector for later use using the following approach:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"718\" height=\"302\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/07\/image-47.png\" alt=\"\" class=\"wp-image-1273\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/07\/image-47.png 718w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/07\/image-47-300x126.png 300w\" sizes=\"auto, (max-width: 718px) 100vw, 718px\" \/><\/figure>\n<\/div>\n\n\n<p> <\/p>\n\n\n\n<p>The key is to use the <a href=\"https:\/\/angular.io\/api\/core\/runInInjectionContext\" target=\"_blank\" rel=\"noopener\" title=\"\"><code>runInInjectionContext<\/code> function<\/a> and pass the injector as a parameter.<\/p>\n\n\n\n<p>For the <code>takeUntilDestroyed <\/code>operator, we can inject a <code><a href=\"https:\/\/angular.io\/api\/core\/DestroyRef\" target=\"_blank\" rel=\"noopener\" title=\"\">DestroyRef<\/a><\/code> in our injection context and then use it as a parameter whenever we need that operator:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"543\" height=\"322\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/07\/image-48.png\" alt=\"\" class=\"wp-image-1274\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/07\/image-48.png 543w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/07\/image-48-300x178.png 300w\" sizes=\"auto, (max-width: 543px) 100vw, 543px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>Problem solved! We can use all these tools from the framework in any place we want, thanks to <code>EnvironmentInjector<\/code> and <code>DestroyRef<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Yesterday, I introduced an injection context, and we saw a few locations where we could inject dependencies. This can be limiting in scenarios where we call a method that creates and returns an Observable outside of an injection context, as we can&#8217;t always initialize observables in a constructor. Does that mean we cannot use the [&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-1271","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\/1271","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=1271"}],"version-history":[{"count":2,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1271\/revisions"}],"predecessor-version":[{"id":1275,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1271\/revisions\/1275"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=1271"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=1271"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=1271"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}