{"id":1004,"date":"2023-05-29T05:00:00","date_gmt":"2023-05-29T12:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=1004"},"modified":"2023-05-22T14:47:11","modified_gmt":"2023-05-22T21:47:11","slug":"hostbinding-and-hostlistener","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/hostbinding-and-hostlistener\/","title":{"rendered":"HostBinding and HostListener"},"content":{"rendered":"\n<p>A few months back, I suggested that Angular developers don&#8217;t <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/when-to-create-a-directive-vs-a-component\/\" target=\"_blank\" rel=\"noopener\" title=\"\">use enough directives and use too many components<\/a>. This is because components quickly become second nature when writing Angular code, while directives are less common and, as a result, can feel more complex at first.<\/p>\n\n\n\n<p>The critical difference between components and directives is that<strong> components have an HTML template<\/strong>, and directives don&#8217;t. That lack of a template can be slightly uncomfortable at first unless you realize that you still have access to HTML attribute bindings but in a different manner.<\/p>\n\n\n\n<p>For instance, let&#8217;s consider the following component template: <\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"523\" height=\"88\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/05\/image-32.png\" alt=\"\" class=\"wp-image-1007\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/05\/image-32.png 523w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/05\/image-32-300x50.png 300w\" sizes=\"auto, (max-width: 523px) 100vw, 523px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>Those two bindings used in a directive would become:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"364\" height=\"279\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/05\/image-33.png\" alt=\"\" class=\"wp-image-1008\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/05\/image-33.png 364w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/05\/image-33-300x230.png 300w\" sizes=\"auto, (max-width: 364px) 100vw, 364px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>In other words, when you see a binding with <code>[]<\/code> in a component, <code>@HostBinding()<\/code> does the same thing in a directive.<\/p>\n\n\n\n<p>For example:  <code>[id]=\"testId\"<\/code> in a component template becomes <code>@HostBinding(\"id\") testId;<\/code> in a directive class.<\/p>\n\n\n\n<p>The same goes for event listeners. This component <code>(click)<\/code> binding:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"81\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/05\/image-34.png\" alt=\"\" class=\"wp-image-1009\"\/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>Becomes the following in a directive:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"328\" height=\"248\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/05\/image-35.png\" alt=\"\" class=\"wp-image-1010\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/05\/image-35.png 328w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/05\/image-35-300x227.png 300w\" sizes=\"auto, (max-width: 328px) 100vw, 328px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>As a recap: <code>[]<\/code> become <code>@HostBinding<\/code> and <code>()<\/code> become <code>@HostListener<\/code>. Those are the same thing. That&#8217;s it. No template is needed. For a <a href=\"https:\/\/blog.angulartraining.com\/tutorial-how-to-create-your-own-angular-directive-3532d7f31fab\" target=\"_blank\" rel=\"noopener\" title=\"\">real-life example of a custom directive, feel free to take a look at this tutorial of mine<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A few months back, I suggested that Angular developers don&#8217;t use enough directives and use too many components. This is because components quickly become second nature when writing Angular code, while directives are less common and, as a result, can feel more complex at first. The critical difference between components and directives is that components [&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,11,7],"tags":[],"class_list":["post-1004","post","type-post","status-publish","format-standard","hentry","category-angular","category-directives","category-template-syntax"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1004","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=1004"}],"version-history":[{"count":4,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1004\/revisions"}],"predecessor-version":[{"id":1012,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1004\/revisions\/1012"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=1004"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=1004"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=1004"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}