|
268 | 268 | |260| [What are the different ways to group form controls?](#what-are-the-different-ways-to-group-form-controls)|
|
269 | 269 | |261| [How do you update specific properties of a form model?](#how-do-you-update-specific-properties-of-a-form-model)|
|
270 | 270 | |262| [What is the purpose of FormBuilder?](#what-is-the-purpose-of-formbuilder)|
|
271 |
| -|263| [](#)| |
272 |
| -|264| [](#)| |
273 |
| -|265| [](#)| |
| 271 | +|263| [How do you verify the model changes in forms?](#how-do-you-verify-the-model-changes-in-forms)| |
| 272 | +|264| [What are the state CSS classes provided by ngModel?](#what-are-the-state-css-classes-provided-by-ngmodel)| |
| 273 | +|265| [How do you reset the form?](#how-do-you-reset-the-form)| |
274 | 274 | |266| [](#)|
|
275 | 275 | |267| [](#)|
|
276 | 276 | |268| [](#)|
|
|
3961 | 3961 |
|
3962 | 3962 |
|
3963 | 3963 | 258. ### What are template driven forms?
|
| 3964 | + Template driven forms are model-driven forms where you write the logic, validations, controls etc, in the template part of the code using directives. They are suitable for simple scenarios and uses two-way binding with [(ngModel)] syntax. |
| 3965 | + For example, you can create register form easily by following the below simple steps, |
| 3966 | +
|
| 3967 | + 1. Import the FormsModule into the Application module's imports array |
| 3968 | + ```js |
| 3969 | + import { BrowserModule } from '@angular/platform-browser'; |
| 3970 | + import { NgModule } from '@angular/core'; |
| 3971 | + import {FormsModule} from '@angular/forms' |
| 3972 | + import { RegisterComponent } from './app.component'; |
| 3973 | + @NgModule({ |
| 3974 | + declarations: [ |
| 3975 | + RegisterComponent, |
| 3976 | + ], |
| 3977 | + imports: [ |
| 3978 | + BrowserModule, |
| 3979 | + FormsModule |
| 3980 | + ], |
| 3981 | + providers: [], |
| 3982 | + bootstrap: [RegisterComponent] |
| 3983 | + }) |
| 3984 | + export class AppModule { } |
| 3985 | + ``` |
| 3986 | + 2. Bind the form from template to the component using ngModel syntax |
| 3987 | + ```html |
| 3988 | + <input type="text" class="form-control" id="name" |
| 3989 | + required |
| 3990 | + [(ngModel)]="model.name" name="name"> |
| 3991 | + ``` |
| 3992 | + 3. Attach NgForm directive to the <form> tag in order to create FormControl instances and register them |
| 3993 | + ```js |
| 3994 | + <form #registerForm="ngForm"> |
| 3995 | + ``` |
| 3996 | + 4. Apply the validation message for form controls |
| 3997 | + ```html |
| 3998 | + <label for="name">Name</label> |
| 3999 | + <input type="text" class="form-control" id="name" |
| 4000 | + required |
| 4001 | + [(ngModel)]="model.name" name="name" |
| 4002 | + #name="ngModel"> |
| 4003 | + <div [hidden]="name.valid || name.pristine" |
| 4004 | + class="alert alert-danger"> |
| 4005 | + Please enter your name |
| 4006 | + </div> |
| 4007 | + ``` |
| 4008 | + 5. Let's submit the form with ngSubmit directive and add type="submit" button at the bottom of the form to trigger form submit. |
| 4009 | + ```html |
| 4010 | + <form (ngSubmit)="onSubmit()" #heroForm="ngForm"> |
| 4011 | + // Form goes here |
| 4012 | + <button type="submit" class="btn btn-success" [disabled]="!registerForm.form.valid">Submit</button> |
| 4013 | + ``` |
| 4014 | + Finally, the completed template-driven registration form will be appeared as follow. |
| 4015 | + ```html |
| 4016 | + <div class="container"> |
| 4017 | + <h1>Registration Form</h1> |
| 4018 | + <form (ngSubmit)="onSubmit()" #registerForm="ngForm"> |
| 4019 | + <div class="form-group"> |
| 4020 | + <label for="name">Name</label> |
| 4021 | + <input type="text" class="form-control" id="name" |
| 4022 | + required |
| 4023 | + [(ngModel)]="model.name" name="name" |
| 4024 | + #name="ngModel"> |
| 4025 | + <div [hidden]="name.valid || name.pristine" |
| 4026 | + class="alert alert-danger"> |
| 4027 | + Please enter your name |
| 4028 | + </div> |
| 4029 | + </div> |
| 4030 | + <button type="submit" class="btn btn-success" [disabled]="!registerForm.form.valid">Submit</button> |
| 4031 | + </form> |
| 4032 | + </div> |
| 4033 | + ``` |
3964 | 4034 |
|
3965 | 4035 | **[⬆ Back to Top](#table-of-contents)**
|
3966 | 4036 |
|
|
4153 | 4223 | ```
|
4154 | 4224 | **[⬆ Back to Top](#table-of-contents)**
|
4155 | 4225 |
|
4156 |
| -263. ### ? |
| 4226 | +263. ### How do you verify the model changes in forms? |
| 4227 | + You can add a getter property(let's say, diagnostic) inside component to return a JSON representation of the model during the development. This is useful to verify whether the values are really flowing from the input box to the model and vice versa or not. |
| 4228 | + ```js |
| 4229 | + export class UserProfileComponent { |
| 4230 | +
|
| 4231 | + model = new User('John', 29, 'Writer'); |
| 4232 | +
|
| 4233 | + // TODO: Remove after the verification |
| 4234 | + get diagnostic() { return JSON.stringify(this.model); } |
| 4235 | + } |
| 4236 | + ``` |
| 4237 | + and add `diagnostic` binding near the top of the form |
| 4238 | + ```html |
| 4239 | + {{diagnostic}} |
| 4240 | + <div class="form-group"> |
| 4241 | + // FormControls goes here |
| 4242 | + </div> |
| 4243 | + ``` |
4157 | 4244 |
|
4158 | 4245 | **[⬆ Back to Top](#table-of-contents)**
|
4159 | 4246 |
|
4160 |
| -264. ### ? |
| 4247 | +264. ### What are the state CSS classes provided by ngModel? |
| 4248 | + The ngModel directive updates the form control with special Angular CSS classes to reflect it's state. Let's find the list of classes in a tabular format, |
| 4249 | +
|
| 4250 | + | Form control state | If true | If false | |
| 4251 | + |---- | --------- | --- | |
| 4252 | + | Visited | ng-touched | ng-untouched | |
| 4253 | + | Value has changed | ng-dirty | ng-pristine | |
| 4254 | + | Value is valid| ng-valid | ng-invalid | |
4161 | 4255 |
|
4162 | 4256 | **[⬆ Back to Top](#table-of-contents)**
|
4163 | 4257 |
|
4164 |
| -265. ### ? |
| 4258 | +265. ### How do you reset the form? |
| 4259 | + In a model-driven form, you can reset the form just by calling the function `reset()` on our form model. |
| 4260 | + For example, you can reset the form model on submission as follows, |
| 4261 | + ```js |
| 4262 | + onSubmit() { |
| 4263 | + if (this.myform.valid) { |
| 4264 | + console.log("Form is submitted"); |
| 4265 | + // Perform business logic here |
| 4266 | + this.myform.reset(); |
| 4267 | + } |
| 4268 | + } |
| 4269 | + ``` |
| 4270 | + Now, your form model resets the form back to its original pristine state. |
4165 | 4271 |
|
4166 | 4272 | **[⬆ Back to Top](#table-of-contents)**
|
4167 | 4273 |
|
|
0 commit comments