Template-Driven Form
Last updated
Last updated
Angular infers the Form-Object (structure) from the DOM. (Better for more simple forms)
Some advantages include:
More difficult to test.
Have an asynchronous data-flow between View and Data model.
Data-flow is way more expensive, expecially in (Model-to-View), when compared to Reactive-driven, since it needs to also call NgModel
methods.
Rely on mutability of the Data Model with two-way binding to update the data model in the Component as changes are made in the Template.
Because there are no unique changes to track on the data model, change detection is less efficient at determining when updates are required.
Form validation is tied to template directives, and must provide custom validator directives that wrap validation functions.
NgModel
without NgForm
Very simple and useless.
In template-driven NgModel
will automatically manage FormControl
for you, so you don't have to create it.
NgModel
If there is no need to create class variables, handle form values ONLY inside the submit function.
Must provide name=""
attributes to the inputs.
@ViewChild
Useful when you need to access the Form not only on submition.
NgModel
two-way bindingWith two-way binding for class variables.
Only basic validation with the help of HTML5 validation.
Angular under the hood will wrap itself on these HTML validators like required
, email
, etc, and do additional stuff.
Just adding them will not prevent the form submition.
(View-to-Model) and (Model-to-view). ()
Writing tests requires a detailed knowledge of the change detection process and an understanding of how directives run on each cycle to ensure that elements are queried, tested or change at the correct time. ()