{"id":2141,"date":"2024-08-07T12:24:39","date_gmt":"2024-08-07T19:24:39","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=2141"},"modified":"2024-08-07T12:24:39","modified_gmt":"2024-08-07T19:24:39","slug":"when-you-dont-need-template-driven-or-reactive-forms","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/when-you-dont-need-template-driven-or-reactive-forms\/","title":{"rendered":"When you don&#8217;t need template-driven or reactive forms"},"content":{"rendered":"\n<p>A common misconception shared by many Angular developers is that if you have a form, you need to use template-driven or reactive forms to handle it.<\/p>\n\n\n\n<p>The truth is, in most cases, simple forms don&#8217;t need any of that. Instead, you can use <a href=\"https:\/\/blog.angulartraining.com\/tutorial-the-magic-of-template-reference-variables-3183f0a0d9d1\" target=\"_blank\" rel=\"noopener\" title=\"\">template reference variables<\/a>. My favorite example is a login form:<\/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=\"248\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/08\/image-1024x248.png\" alt=\"\" class=\"wp-image-2142\" style=\"width:608px;height:auto\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/08\/image-1024x248.png 1024w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/08\/image-300x73.png 300w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/08\/image-768x186.png 768w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/08\/image.png 1414w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>The two template reference variables used in the above example are sufficient to capture the username and password when the user clicks the log-in button. There is no need for extra complexity!<\/p>\n\n\n\n<p>We lose some features along the way, such as reactivity to changes and automatic form validation. For instance, if I use this expression, the value will not change unless the component gets refreshed by another change:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"496\" height=\"86\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/08\/image-1.png\" alt=\"\" class=\"wp-image-2143\" style=\"width:238px;height:auto\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/08\/image-1.png 496w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/08\/image-1-300x52.png 300w\" sizes=\"auto, (max-width: 496px) 100vw, 496px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>This is because no Angular event listener triggers change detection when the form input value changes. But again, for my example of the log-in form, it doesn&#8217;t matter.<\/p>\n\n\n\n<p>You can check out a <a href=\"https:\/\/stackblitz.com\/edit\/at-formgroup-hlhttx?file=src%2Fmain.ts\" target=\"_blank\" rel=\"noopener\" title=\"\">live example here<\/a> where I capture updates when the user clicks a button to emulate some reactivity. For anything more complex than such an example, using <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/reactive-or-template-driven-forms\/\" target=\"_blank\" rel=\"noopener\" title=\"\">template-driven or reactive forms<\/a> makes perfect sense.<\/p>\n\n\n\n<p>If you want to learn more about similar tips and tricks through code challenges delivered to your inbox twice a week, check out the <a href=\"https:\/\/courses.angulartraining.com\/course\/angular-accelerator-program\" target=\"_blank\" rel=\"noopener\" title=\"\">Angular Accelerator program<\/a>. It&#8217;s currently <a href=\"https:\/\/www.linkedin.com\/posts\/achautard_angular-accelerator-application-activity-7226256174050861056-krG0?utm_source=share&amp;utm_medium=member_desktop\" target=\"_blank\" rel=\"noopener\" title=\"\">open for 5 free scholarships<\/a>, and there&#8217;s a 5-day free trial no matter what!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A common misconception shared by many Angular developers is that if you have a form, you need to use template-driven or reactive forms to handle it. The truth is, in most cases, simple forms don&#8217;t need any of that. Instead, you can use template reference variables. My favorite example is a login form: The two [&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,22],"tags":[],"class_list":["post-2141","post","type-post","status-publish","format-standard","hentry","category-angular","category-forms"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/2141","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=2141"}],"version-history":[{"count":1,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/2141\/revisions"}],"predecessor-version":[{"id":2144,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/2141\/revisions\/2144"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=2141"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=2141"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=2141"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}