{"id":2170,"date":"2024-09-18T14:12:01","date_gmt":"2024-09-18T21:12:01","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=2170"},"modified":"2024-09-18T14:12:01","modified_gmt":"2024-09-18T21:12:01","slug":"syntax-tricks-for-angular-html-attribute-bindings","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/syntax-tricks-for-angular-html-attribute-bindings\/","title":{"rendered":"Syntax tricks for Angular HTML attribute bindings"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">What&#8217;s great about Angular is that the framework provides many different features out of the box. Some are well documented and widely known, while others are more obscure and less commonly used, though very useful.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For instance, there&#8217;s a <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/ng-class-for-dynamic-styling\/\" target=\"_blank\" rel=\"noopener\" title=\"\"><code>ngClass<\/code> directive<\/a> to dynamically set classes to an element based on conditions:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"229\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/09\/image-1.png\" alt=\"\" class=\"wp-image-2173\" style=\"width:720px;height:auto\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/09\/image-1.png 1024w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/09\/image-1-300x67.png 300w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/09\/image-1-768x172.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the above example, the button will have an <code>active-btn<\/code> CSS class if <code>isActive<\/code> is true, and a <code>disabled-btn<\/code> CSS class if <code>isDisabled<\/code> is true. That&#8217;s what the directive does.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">But the thing is&#8230; We don&#8217;t need a directive to do that. I never use <code>ngClass<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Instead, I do this:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"109\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/09\/image-4-1024x109.png\" alt=\"\" class=\"wp-image-2176\" style=\"width:763px;height:auto\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/09\/image-4-1024x109.png 1024w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/09\/image-4-300x32.png 300w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/09\/image-4-768x82.png 768w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/09\/image-4-1536x163.png 1536w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/09\/image-4.png 1714w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It works the same way, and I&#8217;d argue that the syntax is slightly easier to understand. No directive needed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">On a different note, if you&#8217;ve ever tried to set the values of non-HTML attributes to an element, such as <code>data-test<\/code> for unit or end-to-end testing, you would find out that this syntax doesn&#8217;t work: <code>[data-test]=\"value\"<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For these &#8220;unknown&#8221; HTML attributes, we need to use the following syntax: <code>[attr.data-test]=\"value\"<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">And then all is fine.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>What&#8217;s great about Angular is that the framework provides many different features out of the box. Some are well documented and widely known, while others are more obscure and less commonly used, though very useful. For instance, there&#8217;s a ngClass directive to dynamically set classes to an element based on conditions: In the above example, [&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,7],"tags":[],"class_list":["post-2170","post","type-post","status-publish","format-standard","hentry","category-angular","category-components","category-template-syntax"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/2170","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=2170"}],"version-history":[{"count":5,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/2170\/revisions"}],"predecessor-version":[{"id":2180,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/2170\/revisions\/2180"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=2170"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=2170"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=2170"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}