{"id":1885,"date":"2023-12-18T05:00:00","date_gmt":"2023-12-18T13:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=1885"},"modified":"2023-12-15T14:52:14","modified_gmt":"2023-12-15T22:52:14","slug":"dynamic-forms-with-formarray","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/dynamic-forms-with-formarray\/","title":{"rendered":"Dynamic forms with FormArray"},"content":{"rendered":"\n<p>We&#8217;ve covered <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/reactive-or-template-driven-forms\/\" target=\"_blank\" rel=\"noopener\" title=\"\">reactive and template-driven forms<\/a> in the past. One of the nice features of reactive forms is the <code><a href=\"https:\/\/angular.dev\/guide\/forms\/reactive-forms#creating-dynamic-forms\" target=\"_blank\" rel=\"noopener\" title=\"\">FormArray<\/a><\/code> class. You can use <code>FormArray<\/code> for a form where the number of inputs is dynamic and can increase or decrease:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"392\" height=\"174\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/12\/dynamicForm.gif\" alt=\"\" class=\"wp-image-1886\"\/><\/figure>\n<\/div>\n\n\n<p>To achieve the above, we create a <code>FormArray<\/code> in our component and add a method so we can append new <code>FormControls<\/code> to that array:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"601\" height=\"194\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/12\/image-14.png\" alt=\"\" class=\"wp-image-1887\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/12\/image-14.png 601w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/12\/image-14-300x97.png 300w\" sizes=\"auto, (max-width: 601px) 100vw, 601px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>Then, we use a <a href=\"https:\/\/blog.angulartraining.com\/angular-17-new-control-flow-syntax-4fbec4772d04\" target=\"_blank\" rel=\"noopener\" title=\"\"><code>@for<\/code> block<\/a> to display all controls of that array in our template, including a button to add new names to the list. Note that we store the index of each control in a local variable <code>i<\/code>:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"783\" height=\"109\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/12\/image-15.png\" alt=\"\" class=\"wp-image-1888\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/12\/image-15.png 783w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/12\/image-15-300x42.png 300w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/12\/image-15-768x107.png 768w\" sizes=\"auto, (max-width: 783px) 100vw, 783px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>We can even add support for the removal of form elements using the <code>removeAt<\/code> method of <code>FormArray<\/code> and the index of the element to remove:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"779\" height=\"134\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/12\/image-16.png\" alt=\"\" class=\"wp-image-1889\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/12\/image-16.png 779w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/12\/image-16-300x52.png 300w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/12\/image-16-768x132.png 768w\" sizes=\"auto, (max-width: 779px) 100vw, 779px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>And that&#8217;s it! We can check that the <code>FormArray<\/code> value is updated correctly with the following expression:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"474\" height=\"36\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/12\/image-17.png\" alt=\"\" class=\"wp-image-1890\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/12\/image-17.png 474w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/12\/image-17-300x23.png 300w\" sizes=\"auto, (max-width: 474px) 100vw, 474px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>You can see that <a href=\"https:\/\/stackblitz.com\/edit\/at-form-array?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%2Fmain.ts,node_modules%2F%40angular%2Fforms%2Findex.d.ts&amp;title=Angular%20Starter\" target=\"_blank\" rel=\"noopener\" title=\"\">code in action on Stackblitz here<\/a>. You can read this excellent <a href=\"https:\/\/blog.angulartraining.com\/managing-nested-and-dynamic-forms-in-angular-cc07f8682ab8\" target=\"_blank\" rel=\"noopener\" title=\"\">guest post from Jennifer Wadella on my blog<\/a> for more information on dynamic forms.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We&#8217;ve covered reactive and template-driven forms in the past. One of the nice features of reactive forms is the FormArray class. You can use FormArray for a form where the number of inputs is dynamic and can increase or decrease: To achieve the above, we create a FormArray in our component and add a method [&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-1885","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\/1885","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=1885"}],"version-history":[{"count":2,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1885\/revisions"}],"predecessor-version":[{"id":1892,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1885\/revisions\/1892"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=1885"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=1885"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=1885"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}