{"id":165,"date":"2018-03-06T03:41:51","date_gmt":"2018-03-06T03:41:51","guid":{"rendered":"http:\/\/www.bhoomabrsr.com\/blog\/?p=165"},"modified":"2019-11-14T03:48:15","modified_gmt":"2019-11-14T03:48:15","slug":"reactive-forms-angular-v5-x","status":"publish","type":"post","link":"https:\/\/www.bhoomabrsr.com\/blog\/index.php\/2018\/03\/06\/reactive-forms-angular-v5-x\/","title":{"rendered":"Reactive Forms &#8211; Angular v5.x"},"content":{"rendered":"<p>In the last article, we discussed Template-driven forms and this article is going to explore about Reactive forms. Both Reactive and template-driven form available under @angular\/forms with module names of <em>ReactiveFormsModule<\/em> and <em>FormsModule<\/em> respectively.Reactive forms required more coding than template-driven way, but these will give more control on forms than template-driven.<br \/>\nReactive Forms:<br \/>\nInstead of angular, we need to create FormGroup and FromControllers mapping between UI and form model.<br \/>\nAdvantages:<br \/>\n1) Field Value and validity updates are always synchronous, so we won&#8217;t encounter the timing issues that sometimes occur in a template-driven form.<br \/>\n2) Reactive forms are easy to write Unit test cases because of most of the business logic available inside of the component.<br \/>\nREACTIVE DRIVEN DIRECTIVES:<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-166\" src=\"https:\/\/www.bhoomabrsr.com\/blog\/wp-content\/uploads\/2018\/03\/reactiveDIR.png\" alt=\"\" width=\"633\" height=\"52\"><\/p>\n<ol>\n<li><em>FormGroupDirective<\/em> &#8211; Binds an existing FormGroup to a DOM element.\n<ul>\n<li>Usage:<strong> [formGroup]=&#8221;farmersForm&#8221;<\/strong><\/li>\n<\/ul>\n<\/li>\n<li><em>FormGroupName <\/em>&#8211; Syncs a nested FormGroup to a DOM element.\n<ul>\n<li>Usage: <strong>formGroupName=&#8221;farmerAddress&#8221;<\/strong><\/li>\n<\/ul>\n<\/li>\n<li><em>FormControlDirective<\/em> &#8211; Syncs a standalone FormControl instance to a form control element.\n<ul>\n<li>Usage: <strong>[formControl]=&#8221;farmerName&#8221;<\/strong><\/li>\n<\/ul>\n<\/li>\n<li><em>FormControlName <\/em>&#8211; Syncs a FormControl in an existing FormGroup to a form control element by name.\n<ul>\n<li>Usage:<strong> formControlName=&#8221;farmerName&#8221;<\/strong><br \/>\n(alternatively)<strong> [formControl]=\u201dfarmersForm.controls.farmerName\u201d<\/strong><\/li>\n<\/ul>\n<\/li>\n<li><em>FormArrayName <\/em>&#8211; Syncs a nested FormArray to a DOM element.\n<ul>\n<li>Usage: <strong><strong>formArrayName=&#8221;grainsTypes&#8221;<\/strong><\/strong><\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p>Simply:<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-172\" src=\"https:\/\/www.bhoomabrsr.com\/blog\/wp-content\/uploads\/2018\/03\/reactiveform.jpg\" alt=\"\" width=\"3600\" height=\"3600\"><br \/>\nExample: <a href=\"https:\/\/plnkr.co\/edit\/qtu7JHuGu92XbWspzvJ9?p=preview\">Live Example<\/a><\/p>\n<p><script data-ad-client=\"ca-pub-8821695692204473\" async=\"\" src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the last article, we discussed Template-driven forms and this article is going to explore about Reactive forms. Both Reactive and template-driven form available under @angular\/forms with module names of ReactiveFormsModule and FormsModule respectively.Reactive forms&hellip; <a href=\"https:\/\/www.bhoomabrsr.com\/blog\/index.php\/2018\/03\/06\/reactive-forms-angular-v5-x\/\">More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,4,5],"tags":[],"class_list":["post-165","post","type-post","status-publish","format-standard","hentry","category-angular","category-angular-5-2-6","category-angular-v5-x"],"_links":{"self":[{"href":"https:\/\/www.bhoomabrsr.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/165","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bhoomabrsr.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bhoomabrsr.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bhoomabrsr.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bhoomabrsr.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=165"}],"version-history":[{"count":2,"href":"https:\/\/www.bhoomabrsr.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/165\/revisions"}],"predecessor-version":[{"id":212,"href":"https:\/\/www.bhoomabrsr.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/165\/revisions\/212"}],"wp:attachment":[{"href":"https:\/\/www.bhoomabrsr.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=165"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bhoomabrsr.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=165"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bhoomabrsr.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=165"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}