{"id":6438,"date":"2021-03-13T20:25:34","date_gmt":"2021-03-13T20:55:34","guid":{"rendered":"https:\/\/ultering.com\/it4us\/?p=6438"},"modified":"2022-10-15T12:09:16","modified_gmt":"2022-10-15T12:39:16","slug":"angular-from-scratch-tutorial-step-7-forms","status":"publish","type":"post","link":"https:\/\/ultering.com\/it4us\/?p=6438","title":{"rendered":"Angular From Scratch Tutorial &#8211; Step 7: Forms"},"content":{"rendered":"<p><a href=\"https:\/\/ultering.com\/it4us\/?p=7175\">Angular From Scratch Tutorial &#8211; Index<\/a><\/p>\n<p><a href=\"https:\/\/ultering.com\/it4us\/?p=6426\" target=\"_blank\" rel=\"noopener\">PREVIOUS: Angular From Scratch Tutorial \u2013 Step 6: Service And Dependency Injection<\/a><\/p>\n<p>&nbsp;<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_83 counter-hierarchy ez-toc-counter ez-toc-white ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a2d533b918fe\" class=\"ez-toc-cssicon-toggle-label\"><span class><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\" \/><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\" \/><\/svg><\/span><\/span><\/label><input type=\"checkbox\" id=\"ez-toc-cssicon-toggle-item-6a2d533b918fe\" checked aria-label=\"Toggle\"><nav><ul class=\"ez-toc-list ez-toc-list-level-1 \"><li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/ultering.com\/it4us\/?p=6438\/#TARGET\">TARGET<\/a><ul class=\"ez-toc-list-level-3\"><li class=\"ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/ultering.com\/it4us\/?p=6438\/#Summary\">Summary<\/a><\/li><\/ul><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/ultering.com\/it4us\/?p=6438\/#Choosing_an_approach\">Choosing an approach<\/a><ul class=\"ez-toc-list-level-3\"><li class=\"ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/ultering.com\/it4us\/?p=6438\/#Key_differences\">Key differences<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/ultering.com\/it4us\/?p=6438\/#Common_form_foundation_classes\">Common form foundation classes<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/ultering.com\/it4us\/?p=6438\/#Template-driven_models\">Template-driven models<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/ultering.com\/it4us\/?p=6438\/#_Reactive_forms\">&nbsp;Reactive forms<\/a><\/li><\/ul><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/ultering.com\/it4us\/?p=6438\/#Setup\">Setup<\/a><ul class=\"ez-toc-list-level-3\"><li class=\"ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/ultering.com\/it4us\/?p=6438\/#%3EPROBLEM\">&gt;PROBLEM<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/ultering.com\/it4us\/?p=6438\/#%3ESOLUTION\">&gt;SOLUTION<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/ultering.com\/it4us\/?p=6438\/#%3EENV\">&gt;ENV<\/a><\/li><\/ul><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/ultering.com\/it4us\/?p=6438\/#Accessing_field_values_with_ngForm\">Accessing field values with ngForm<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/ultering.com\/it4us\/?p=6438\/#Referencing_Snippets\">Referencing Snippets<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/ultering.com\/it4us\/?p=6438\/#ngSubmit\">ngSubmit<\/a><ul class=\"ez-toc-list-level-3\"><li class=\"ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/ultering.com\/it4us\/?p=6438\/#Snippets\">Snippets<\/a><\/li><\/ul><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/ultering.com\/it4us\/?p=6438\/#i\">&lt;select&gt;<\/a><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"TARGET\"><\/span>TARGET<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Revision for working with forms when you have already worked with Angular but after some time you need to refresh your memory.<\/p>\n<p>The theory comes from <a href=\"https:\/\/angular.io\/guide\/forms-overview\">Introduction to forms in Angular doc.<\/a><br>\nThis page is a summary for fast revision.<br>\nIf it is your first time, you&#8217;d better go to <a href=\"https:\/\/angular.io\/docs\">Angular Documentation<\/a>.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Summary\"><\/span>Summary<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>One of the advantages of working directly with form control objects is that value and validity updates are always synchronous and under your control.<br>\nAngular offers two form-building technologies: reactive forms and template-driven forms.<br>\nThese two belong to the @angular\/forms library and share a series of form control classes.<br>\nThey even have their own modules: ReactiveFormsModule and FormsModule.<br>\nReactive forms provide synchronous access to the data model, immutability with observable operators, and change tracking through observable streams.<br>\nTemplate-driven forms let direct access modify data in your template, but are less explicit than reactive forms because they rely on directives embedded in the template, along with mutable data to track changes asynchronously.<\/p>\n<p><noscript><img decoding=\"async\" class=\"alignnone size-full wp-image-7206\" src=\"https:\/\/ultering.com\/it4us\/wp-content\/uploads\/angular_ML44649_01.jpg\" alt width=\"819\" height=\"239\" srcset=\"https:\/\/ultering.com\/it4us\/wp-content\/uploads\/angular_ML44649_01.jpg 819w, https:\/\/ultering.com\/it4us\/wp-content\/uploads\/angular_ML44649_01-300x88.jpg 300w, https:\/\/ultering.com\/it4us\/wp-content\/uploads\/angular_ML44649_01-768x224.jpg 768w\" sizes=\"(max-width: 819px) 100vw, 819px\"><\/noscript><img decoding=\"async\" class=\"alignnone size-full wp-image-7206 lazyload\" src=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20819%20239%22%3E%3C%2Fsvg%3E\" alt width=\"819\" height=\"239\" srcset=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20819%20239%22%3E%3C%2Fsvg%3E 819w\" sizes=\"(max-width: 819px) 100vw, 819px\" data-srcset=\"https:\/\/ultering.com\/it4us\/wp-content\/uploads\/angular_ML44649_01.jpg 819w, https:\/\/ultering.com\/it4us\/wp-content\/uploads\/angular_ML44649_01-300x88.jpg 300w, https:\/\/ultering.com\/it4us\/wp-content\/uploads\/angular_ML44649_01-768x224.jpg 768w\" data-src=\"https:\/\/ultering.com\/it4us\/wp-content\/uploads\/angular_ML44649_01.jpg\"><\/p>\n<p><span style=\"font-size: 10pt;\">@SEE: more at:<\/span><br>\n<a href=\"https:\/\/angular-templates.io\/tutorials\/about\/angular-forms-and-validations\">Learn Angular with free step by step angular tutorials [ML44649]<\/a><\/p>\n<p>&nbsp;<\/p>\n<h2 id=\"choosing-an-approach\"><span class=\"ez-toc-section\" id=\"Choosing_an_approach\"><\/span>Choosing an approach<i class=\"material-icons\"><\/i><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Reactive forms and template-driven forms process and manage form data differently. Each approach offers different advantages.<\/p>\n<table>\n<thead>\n<tr>\n<th align=\"left\">FORMS<\/th>\n<th align=\"left\">DETAILS<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td align=\"left\">Reactive forms<\/td>\n<td align=\"left\">Provide direct, explicit access to the underlying forms object model. Compared to template-driven forms, they are more robust: they&#8217;re more scalable, reusable, and testable. If forms are a key part of your application, or you&#8217;re already using reactive patterns for building your application, use reactive forms.<\/td>\n<\/tr>\n<tr>\n<td align=\"left\">Template-driven forms<\/td>\n<td align=\"left\">Rely on directives in the template to create and manipulate the underlying object model. They are useful for adding a simple form to an app, such as an email list signup form. They&#8217;re straightforward to add to an app, but they don&#8217;t scale as well as reactive forms. If you have very basic form requirements and logic that can be managed solely in the template, template-driven forms could be a good fit.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<h3 id=\"key-differences\"><span class=\"ez-toc-section\" id=\"Key_differences\"><\/span>Key differences<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><noscript><img decoding=\"async\" class=\"alignnone size-full wp-image-7144\" src=\"https:\/\/ultering.com\/it4us\/wp-content\/uploads\/angular_doc_forms_key_diffferences.jpg\" alt width=\"869\" height=\"368\" srcset=\"https:\/\/ultering.com\/it4us\/wp-content\/uploads\/angular_doc_forms_key_diffferences.jpg 869w, https:\/\/ultering.com\/it4us\/wp-content\/uploads\/angular_doc_forms_key_diffferences-300x127.jpg 300w, https:\/\/ultering.com\/it4us\/wp-content\/uploads\/angular_doc_forms_key_diffferences-768x325.jpg 768w\" sizes=\"(max-width: 869px) 100vw, 869px\"><\/noscript><img decoding=\"async\" class=\"alignnone size-full wp-image-7144 lazyload\" src=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20869%20368%22%3E%3C%2Fsvg%3E\" alt width=\"869\" height=\"368\" srcset=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20869%20368%22%3E%3C%2Fsvg%3E 869w\" sizes=\"(max-width: 869px) 100vw, 869px\" data-srcset=\"https:\/\/ultering.com\/it4us\/wp-content\/uploads\/angular_doc_forms_key_diffferences.jpg 869w, https:\/\/ultering.com\/it4us\/wp-content\/uploads\/angular_doc_forms_key_diffferences-300x127.jpg 300w, https:\/\/ultering.com\/it4us\/wp-content\/uploads\/angular_doc_forms_key_diffferences-768x325.jpg 768w\" data-src=\"https:\/\/ultering.com\/it4us\/wp-content\/uploads\/angular_doc_forms_key_diffferences.jpg\"><\/p>\n<h3><\/h3>\n<h3 id=\"common-form-foundation-classes\"><span class=\"ez-toc-section\" id=\"Common_form_foundation_classes\"><\/span>Common form foundation classes<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><noscript><img decoding=\"async\" class=\"alignnone size-full wp-image-7145\" src=\"https:\/\/ultering.com\/it4us\/wp-content\/uploads\/angular_doc_forms_common_classes.jpg\" alt width=\"949\" height=\"398\" srcset=\"https:\/\/ultering.com\/it4us\/wp-content\/uploads\/angular_doc_forms_common_classes.jpg 949w, https:\/\/ultering.com\/it4us\/wp-content\/uploads\/angular_doc_forms_common_classes-300x126.jpg 300w, https:\/\/ultering.com\/it4us\/wp-content\/uploads\/angular_doc_forms_common_classes-768x322.jpg 768w\" sizes=\"(max-width: 949px) 100vw, 949px\"><\/noscript><img decoding=\"async\" class=\"alignnone size-full wp-image-7145 lazyload\" src=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20949%20398%22%3E%3C%2Fsvg%3E\" alt width=\"949\" height=\"398\" srcset=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20949%20398%22%3E%3C%2Fsvg%3E 949w\" sizes=\"(max-width: 949px) 100vw, 949px\" data-srcset=\"https:\/\/ultering.com\/it4us\/wp-content\/uploads\/angular_doc_forms_common_classes.jpg 949w, https:\/\/ultering.com\/it4us\/wp-content\/uploads\/angular_doc_forms_common_classes-300x126.jpg 300w, https:\/\/ultering.com\/it4us\/wp-content\/uploads\/angular_doc_forms_common_classes-768x322.jpg 768w\" data-src=\"https:\/\/ultering.com\/it4us\/wp-content\/uploads\/angular_doc_forms_common_classes.jpg\"><\/p>\n<h3><span class=\"ez-toc-section\" id=\"Template-driven_models\"><\/span>Template-driven models<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>&nbsp;<\/p>\n<pre class>import { Component, OnInit } from '@angular\/core';\nimport { FormControl } from '@angular\/forms';\n\n@Component({\n  selector: 'app-forms2',\n  template: `\n    Favorite Color: &lt;input type=\"text\" [(ngModel)]=\"favoriteColor\"&gt;\n  `,\n  styleUrls: ['.\/forms2.component.sass']\n})\nexport class Forms2Component implements OnInit {\n\n  favoriteColor = '';\n\n  constructor() { }\n\n  ngOnInit(): void {\n  }\n\n}\n\n<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"_Reactive_forms\"><\/span>&nbsp;Reactive forms<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>&nbsp;<\/p>\n<pre class>import { Component, OnInit } from '@angular\/core'; \nimport { FormControl } from '@angular\/forms'; \n@Component({ \n\tselector: 'app-forms1', \n\ttemplate: ` Favorite Color: &lt;input type=\"text\" [formControl]=\"favoriteColorControl\"&gt; `, \n\tstyleUrls: ['.\/forms1.component.sass'] }) \nexport class Forms1Component implements OnInit { \n\tfavoriteColorControl = new FormControl(''); \n\tconstructor() { } \n\tngOnInit(): void { } \n}\n<\/pre>\n<p>Additional Links<\/p>\n<p><a href=\"https:\/\/stackblitz.com\/run?file=src%2Fapp%2Fprofile-editor%2Fprofile-editor.component.html\">stackblitz reactive form<\/a><\/p>\n<p><a href=\"https:\/\/stackblitz.com\/edit\/angular-hero-form-example?file=src%2Fapp%2Fapp.component.ts,src%2Fapp%2Fapp.component.html,src%2Fapp%2Fhero-form%2Fhero-form.component.html\">stackblitz template-driven<\/a><\/p>\n<p>&nbsp;<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Setup\"><\/span>Setup<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3 data-original-attrs=\"{&quot;style&quot;:&quot;&quot;}\"><span class=\"ez-toc-section\" id=\"%3EPROBLEM\"><\/span>&gt;PROBLEM<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Running angular using ngModels, you get:<\/p>\n<p>error NG8002: Can&#8217;t bind to &#8216;formControl&#8217; since it isn&#8217;t a known property of &#8216;input&#8217;.<\/p>\n<p>Example, implementing the following code:<\/p>\n<h2><noscript><img decoding=\"async\" class=\"alignnone size-full wp-image-7148\" src=\"https:\/\/ultering.com\/it4us\/wp-content\/uploads\/angular_doc_reactive_form1.jpg\" alt width=\"1000\" height=\"541\" srcset=\"https:\/\/ultering.com\/it4us\/wp-content\/uploads\/angular_doc_reactive_form1.jpg 1000w, https:\/\/ultering.com\/it4us\/wp-content\/uploads\/angular_doc_reactive_form1-300x162.jpg 300w, https:\/\/ultering.com\/it4us\/wp-content\/uploads\/angular_doc_reactive_form1-768x415.jpg 768w\" sizes=\"(max-width: 1000px) 100vw, 1000px\"><\/noscript><img decoding=\"async\" class=\"alignnone size-full wp-image-7148 lazyload\" src=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%201000%20541%22%3E%3C%2Fsvg%3E\" alt width=\"1000\" height=\"541\" srcset=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%201000%20541%22%3E%3C%2Fsvg%3E 1000w\" sizes=\"(max-width: 1000px) 100vw, 1000px\" data-srcset=\"https:\/\/ultering.com\/it4us\/wp-content\/uploads\/angular_doc_reactive_form1.jpg 1000w, https:\/\/ultering.com\/it4us\/wp-content\/uploads\/angular_doc_reactive_form1-300x162.jpg 300w, https:\/\/ultering.com\/it4us\/wp-content\/uploads\/angular_doc_reactive_form1-768x415.jpg 768w\" data-src=\"https:\/\/ultering.com\/it4us\/wp-content\/uploads\/angular_doc_reactive_form1.jpg\"><\/h2>\n<p>You get:<\/p>\n<h2><\/h2>\n<h2><noscript><img decoding=\"async\" class=\"alignnone size-full wp-image-7149\" src=\"https:\/\/ultering.com\/it4us\/wp-content\/uploads\/angular_doc_reactive_form1_fail.jpg\" alt width=\"1378\" height=\"479\" srcset=\"https:\/\/ultering.com\/it4us\/wp-content\/uploads\/angular_doc_reactive_form1_fail.jpg 1378w, https:\/\/ultering.com\/it4us\/wp-content\/uploads\/angular_doc_reactive_form1_fail-300x104.jpg 300w, https:\/\/ultering.com\/it4us\/wp-content\/uploads\/angular_doc_reactive_form1_fail-768x267.jpg 768w\" sizes=\"(max-width: 1378px) 100vw, 1378px\"><\/noscript><img decoding=\"async\" class=\"alignnone size-full wp-image-7149 lazyload\" src=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%201378%20479%22%3E%3C%2Fsvg%3E\" alt width=\"1378\" height=\"479\" srcset=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%201378%20479%22%3E%3C%2Fsvg%3E 1378w\" sizes=\"(max-width: 1378px) 100vw, 1378px\" data-srcset=\"https:\/\/ultering.com\/it4us\/wp-content\/uploads\/angular_doc_reactive_form1_fail.jpg 1378w, https:\/\/ultering.com\/it4us\/wp-content\/uploads\/angular_doc_reactive_form1_fail-300x104.jpg 300w, https:\/\/ultering.com\/it4us\/wp-content\/uploads\/angular_doc_reactive_form1_fail-768x267.jpg 768w\" data-src=\"https:\/\/ultering.com\/it4us\/wp-content\/uploads\/angular_doc_reactive_form1_fail.jpg\"><\/h2>\n<p>&nbsp;<\/p>\n<h3 data-original-attrs=\"{&quot;style&quot;:&quot;&quot;}\"><span class=\"ez-toc-section\" id=\"%3ESOLUTION\"><\/span>&gt;SOLUTION<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>There are at least two things to be observed.<\/p>\n<p><b>1. Require modules setup.<\/b><\/p>\n<p>Set the required modules in app.modules.ts:<\/p>\n<p>import { FormsModule, ReactiveFormsModule } from &#8220;@angular\/forms&#8221;;<\/p>\n<p>&#8230;<\/p>\n<p>@NgModule({<\/p>\n<p>declarations: [<\/p>\n<p>\/\/&#8230;<\/p>\n<p>],<\/p>\n<p>imports: [<\/p>\n<p>\/\/&#8230;<\/p>\n<p>FormsModule,<\/p>\n<p>ReactiveFormsModule,<\/p>\n<p>],<\/p>\n<p><b>*** IMPORTANT NOTE:<\/b><\/p>\n<p>If you are using a page under its specific module, declare in that module two.<\/p>\n<p>Example:<\/p>\n<p>app<\/p>\n<p>+&#8211;forms-lab<\/p>\n<p>+&#8211; forms1<\/p>\n<p>&#8212; forms-lab.module.ts<\/p>\n<p>Edit forms-lab.module.ts and declare both modules referred to above.<\/p>\n<p><b>2. Make sure that you have not overwritten the angular modules:&nbsp;<\/b><\/p>\n<p>It is something easy to happen, for instance, if you create a module called forms, as it is:<\/p>\n<p><i><br>\nng g m forms<\/i><\/p>\n<p>This command will generate a FormsModule that overrides the angular&#8217;s module.<br>\nThis conflict causes the same issue.<\/p>\n<p>If it happened, delete, and recreate it with another name.<\/p>\n<p>@SEE:<\/p>\n<p><a href=\"https:\/\/www.blogger.com\/u\/1\/blog\/post\/edit\/26129694655928772\/782144305983518528#\" data-original-attrs=\"{&quot;data-original-href&quot;:&quot;https:\/\/stackoverflow.com\/questions\/43220348\/cant-bind-to-formcontrol-since-it-isnt-a-known-property-of-input-angular&quot;}\">https:\/\/stackoverflow.com\/questions\/43220348\/cant-bind-to-formcontrol-since-it-isnt-a-known-property-of-input-angular<\/a><\/p>\n<h3 data-original-attrs=\"{&quot;style&quot;:&quot;&quot;}\"><span class=\"ez-toc-section\" id=\"%3EENV\"><\/span>&gt;ENV<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Angular CLI: 13.2.2<br>\nNode: 16.13.1<br>\nPackage Manager: npm 8.5.4<br>\nOS: win32 x64<\/p>\n<h2><\/h2>\n<h2><span class=\"ez-toc-section\" id=\"Accessing_field_values_with_ngForm\"><\/span>Accessing field values with ngForm<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>&nbsp;<\/p>\n<pre class>  &lt;h6&gt;&lt;a href=\"https:\/\/angular.io\/api\/forms\/NgForm\"&gt;https:\/\/angular.io\/api\/forms\/NgForm&lt;\/a&gt;&lt;\/h6&gt;\n  &lt;form #itemForm=\"ngForm\" (ngSubmit)=\"onSubmit(itemForm)\"&gt;\n    &lt;label for=\"name\"&gt;Name&lt;\/label&gt;&amp;nbsp;\n    &lt;input type=\"text\" id=\"name\" class=\"form-control\" name=\"name\" ngModel required \/&gt;&amp;nbsp;&amp;nbsp;\n    &lt;button type=\"submit\"&gt;Submit&lt;\/button&gt;\n  &lt;\/form&gt;\n  \n  import {NgForm, FormGroup} from '@angular\/forms';\n  \n  onSubmit(f: NgForm): void {\n    console.log(f.value);  \/\/ { first: '', last: '' }\n    console.log(f.valid);  \/\/ false\n    alert(f.value.emailid);\n  }\n\n<\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Referencing_Snippets\"><\/span>Referencing Snippets<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<pre class>&gt;snippet #1\n- .ts file:\n    \/\/ convenience getter for easy access to form fields\n    get f() { return this.loginForm.controls; }\n\n- .html file:\n&lt;div *ngIf=\"submitted &amp;&amp; f.username.errors\" class=\"invalid-feedback\"&gt;\n\t&lt;div *ngIf=\"f.username.errors.required\"&gt;Username is required&lt;\/div&gt;\n&lt;\/div&gt;\n\n\n&gt;snippet #2\n- .html file:\n    &lt;form\n      *ngIf=\"!isLoggedIn\"\n      name=\"form\"\n      (ngSubmit)=\"f.form.valid &amp;&amp; onSubmit()\"\n      #f=\"ngForm\"\n      novalidate\n    &gt;\n<\/pre>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/angular.io\/api\/forms\/NgForm\">From Angular doc: NgForm<\/a><\/p>\n<p>&nbsp;<\/p>\n<pre class>import {Component} from '@angular\/core';\nimport {NgForm} from '@angular\/forms';\n\n@Component({\n  selector: 'example-app',\n  template: `\n    &lt;form #f=\"ngForm\" (ngSubmit)=\"onSubmit(f)\" novalidate&gt;\n      &lt;input name=\"first\" ngModel required #first=\"ngModel\"&gt;\n      &lt;input name=\"last\" ngModel&gt;\n      &lt;button&gt;Submit&lt;\/button&gt;\n    &lt;\/form&gt;\n\n    &lt;p&gt;First name value: {{ first.value }}&lt;\/p&gt;\n    &lt;p&gt;First name valid: {{ first.valid }}&lt;\/p&gt;\n    &lt;p&gt;Form value: {{ f.value | json }}&lt;\/p&gt;\n    &lt;p&gt;Form valid: {{ f.valid }}&lt;\/p&gt;\n  `,\n})\nexport class SimpleFormComp {\n  onSubmit(f: NgForm) {\n    console.log(f.value);  \/\/ { first: '', last: '' }\n    console.log(f.valid);  \/\/ false\n  }\n}\n<\/pre>\n<p>&nbsp;<\/p>\n<h2><span class=\"ez-toc-section\" id=\"ngSubmit\"><\/span>ngSubmit<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>SEE:<br>\n<a href=\"https:\/\/www.techiediaries.com\/build-angular-form\/\">&#8211; Build angular 13 forms with example [ML44183]<\/a><br>\n<a href=\"https:\/\/angular.io\/api\/forms\/FormGroupDirective\">&#8211; Angular: FormGroupDirective<\/a><\/p>\n<h3><span class=\"ez-toc-section\" id=\"Snippets\"><\/span>Snippets<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre class>- .html file:\n\n&lt;form\n      *ngIf=\"!isLoggedIn\"\n      name=\"form\"\n      (ngSubmit)=\"f.form.valid &amp;&amp; onSubmit()\"\n      #f=\"ngForm\"\n      novalidate\n    &gt;\n\n- .ts file:\n  onSubmit(): void {\n    const { username, password } = this.form;\n\n    this.authService.login(username, password).subscribe({\n      next: data =&gt; {\n        this.tokenStorage.saveToken(data.accessToken);\n        this.tokenStorage.saveUser(data);\n\n        this.isLoginFailed = false;\n        this.isLoggedIn = true;\n        this.roles = this.tokenStorage.getUser().roles;\n        this.reloadPage();\n      },\n      error: err =&gt; {\n        this.errorMessage = err.error.message;\n        this.isLoginFailed = true;\n      }\n    });\n  }\n<\/pre>\n<p>&nbsp;<\/p>\n<h2><span class=\"ez-toc-section\" id=\"i\"><\/span>&lt;select&gt;<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<pre class>&gt;component.ts\n\nactivities: any;\nactivity: string;\n\n  constructor(\n    private activityService: ActivityService,\n    private todosService: TodosService,\n  ) {\n    activityService.load(this.email, this.stoken).subscribe((objs) =&gt; {\n      this.activities = [];\n      for (let i = 0; i &lt; objs.activities.length; i++) {\n        this.activities[i] = objs.activities[i];\n      }\n    });\n    this.activity = 'all activities';\n  }\n\n\n  newTodoSubmit(): void {\n    alert(this.activity);\n  }\n\n\n&gt;component.html\n\n&lt;select id=\"activities_id\" class=\"form-select caret\" [(ngModel)]=\"activity\"\n  style=\"border-color: transparent !important; border-width: 0px !important; margin-top: 0px; font-size: 12px !important; width: 120px !important;\"&gt;\n  &lt;option value=\"{{activity}}\" selected&gt;all activities&lt;\/option&gt;\n  &lt;option value=\"{{activity.name}}\" *ngFor=\"let activity of activities\"&gt;{{activity.name}}&lt;\/option&gt;\n&lt;\/select&gt;\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/ultering.com\/it4us\/?p=7175\">Angular From Scratch Tutorial &#8211; Index<\/a><\/p>\n<p><a href=\"https:\/\/ultering.com\/it4us\/?p=6426\" target=\"_blank\" rel=\"noopener\">PREVIOUS: Angular From Scratch Tutorial \u2013 Step 6: Service And Dependency Injection<\/a><\/p>\n<p>&nbsp;<\/p>\n<!--CusAds0-->\n<div style=\"font-size: 0px; height: 0px; line-height: 0px; margin: 0; padding: 0; clear: both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>Angular From Scratch Tutorial &#8211; Index PREVIOUS: Angular From Scratch Tutorial \u2013 Step 6: Service And Dependency Injection &nbsp; TARGET Revision for working with forms when you have already worked with Angular but after some time you need to refresh your memory. The theory comes from Introduction to forms in Angular doc. This page is &#8230; <a href=\"https:\/\/ultering.com\/it4us\/?p=6438\" class=\"more-link\">Read More<span class=\"screen-reader-text\"> &#8220;Angular From Scratch Tutorial &#8211; Step 7: Forms&#8221;<\/span> &raquo;<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"footnotes":""},"categories":[192,26],"tags":[],"class_list":["post-6438","post","type-post","status-publish","format-standard","hentry","category-angular","category-javascript"],"_links":{"self":[{"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=\/wp\/v2\/posts\/6438","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6438"}],"version-history":[{"count":6,"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=\/wp\/v2\/posts\/6438\/revisions"}],"predecessor-version":[{"id":7209,"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=\/wp\/v2\/posts\/6438\/revisions\/7209"}],"wp:attachment":[{"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6438"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6438"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6438"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}