{"id":1714,"date":"2023-11-03T05:00:00","date_gmt":"2023-11-03T12:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=1714"},"modified":"2024-10-21T12:47:41","modified_gmt":"2024-10-21T19:47:41","slug":"dependency-injection-elementref","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/dependency-injection-elementref\/","title":{"rendered":"Dependency Injection: ElementRef"},"content":{"rendered":"\n<p>We use dependency injection daily to inject services in our Angular &#8220;objects,&#8221; such as components, directives, pipes, and other services. <\/p>\n\n\n\n<p>Angular does offer other kinds of injectables, though, and one of those is the <code><a href=\"https:\/\/angular.dev\/api\/core\/ElementRef?tab=description\" target=\"_blank\" rel=\"noopener\" title=\"\">ElementRef<\/a><\/code>. Its primary purpose is to give us direct access to the native DOM element of a component or a directive.<\/p>\n\n\n\n<p>Here&#8217;s a code example (you can <a href=\"https:\/\/stackblitz.com\/edit\/stackblitz-starters-tqud7q?description=An%20angular-cli%20project%20based%20on%20@angular\/animations,%20@angular\/common,%20@angular\/compiler,%20@angular\/core,%20@angular\/forms,%20@angular\/platform-browser,%20@angular\/platform-browser-dynamic,%20@angular\/router,%20core-js,%20rxjs,%20tslib%20and%20zone.js&amp;file=src%2Fhighlight.directive.ts,src%2Fmain.ts&amp;title=Angular%20Starter\" target=\"_blank\" rel=\"noopener\" title=\"\">test it in Stackblitz here<\/a>):<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"591\" height=\"297\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/11\/image-1.png\" alt=\"\" class=\"wp-image-1715\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/11\/image-1.png 591w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/11\/image-1-300x151.png 300w\" sizes=\"auto, (max-width: 591px) 100vw, 591px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>The above code changes the background color of the HTML element to red. It&#8217;s important to note that the above implementation is <strong>not ideal<\/strong>. We should use Angular data bindings for such a use case, which are a lot more readable (<a href=\"https:\/\/stackblitz.com\/edit\/stackblitz-starters-mkquqt?description=An%20angular-cli%20project%20based%20on%20@angular\/animations,%20@angular\/common,%20@angular\/compiler,%20@angular\/core,%20@angular\/forms,%20@angular\/platform-browser,%20@angular\/platform-browser-dynamic,%20@angular\/router,%20core-js,%20rxjs,%20tslib%20and%20zone.js&amp;file=src%2Fhighlight.directive.ts,src%2Fmain.ts&amp;title=Angular%20Starter\" target=\"_blank\" rel=\"noopener\" title=\"\">code example in Stackblitz here<\/a>):<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"580\" height=\"270\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/11\/image-2.png\" alt=\"\" class=\"wp-image-1716\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/11\/image-2.png 580w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/11\/image-2-300x140.png 300w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>The Angular team <a href=\"https:\/\/angular.io\/api\/core\/ElementRef#properties\" target=\"_blank\" rel=\"noopener\" title=\"\">doesn&#8217;t recommend using this API<\/a> either:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"581\" height=\"193\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/11\/image-3.png\" alt=\"\" class=\"wp-image-1717\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/11\/image-3.png 581w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/11\/image-3-300x100.png 300w\" sizes=\"auto, (max-width: 581px) 100vw, 581px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>So when should we use <code>ElementRef<\/code>? Just like <code><a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/changedetectorref-markforcheck\/\" target=\"_blank\" rel=\"noopener\" title=\"\">markForCheck<\/a><\/code>, it&#8217;s a last resort option,<a href=\"https:\/\/blog.angulartraining.com\/how-to-pick-the-right-dependencies-for-your-angular-application-328ce637b982\" target=\"_blank\" rel=\"noopener\" title=\"\"> and we should use it when everything else fails. The only use case I would consider for ElementRef is when I have to interact with a non-Angular 3rd-party library,<\/a> such as a chart or a map library, after carefully checking that Angular wrappers are not available on <a href=\"https:\/\/www.npmjs.com\/\" target=\"_blank\" rel=\"noopener\" title=\"\">npm<\/a> (or such wrappers do not pass <a href=\"https:\/\/blog.angulartraining.com\/how-to-pick-the-right-dependencies-for-your-angular-application-328ce637b982\" target=\"_blank\" rel=\"noopener\" title=\"\">my tests for good, reliable dependencies<\/a>)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We use dependency injection daily to inject services in our Angular &#8220;objects,&#8221; such as components, directives, pipes, and other services. Angular does offer other kinds of injectables, though, and one of those is the ElementRef. Its primary purpose is to give us direct access to the native DOM element of a component or a directive. [&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,8,26],"tags":[],"class_list":["post-1714","post","type-post","status-publish","format-standard","hentry","category-angular","category-components","category-dependency-injection"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1714","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=1714"}],"version-history":[{"count":3,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1714\/revisions"}],"predecessor-version":[{"id":2207,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1714\/revisions\/2207"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=1714"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=1714"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=1714"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}