Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/dennisxzx/angular-template-driven-form-example


https://github.com/dennisxzx/angular-template-driven-form-example

Last synced: about 1 month ago
JSON representation

Awesome Lists containing this project

README

        

### Angular Template Driven Form Example

In order to use Angular template driven form, you need to first import `FormsModule` into your Angular app.

When you have the `FormsModule` imported, Angular would look for tag and internally add an `NgForm` directive `` and create an instance of `NgForm`. You can access this instance by using a template reference ``

To hook up a form input with the `ngForm` instance, you need to add `NgModel` to the form input and also give it a `name` attribute.

However, if you need to enable two-way data binding, you would need to use the `[(ngModel)]="userSettings.name"` in the template.

### Validation

Angular form comes with these CSS classes for validation purpose.

- `ng-untouched` and `ng-touched`
- `ng-pristine` (unmodified) and `ng-dirty` (modified)
- `ng-valid` and `ng-invalid`

Also `NgModel` has properties associated with the CSS classes, for example, `userSettingsForm.valid` and `userSettingsForm.untouched`.