Understanding Template Forms
In this lesson, we'll take a moment to understand what's going on behind the scenes with template forms.
We'll cover the following
Believe it or not, template forms aren’t all that different from reactive forms. Under the hood, template forms are using form groups and controls. It might seem mysterious because we haven’t imported those classes.
When we use template forms, Angular will scan your template for <form>
elements. If it finds one, it will automatically create a form group and bind it for you. This is why we never had to do it ourselves.
The same applies to <input>
elements. If Angular finds an <input>
element, it will create a form control and bind it. This is the power of template forms. Rather than configuring the form from the component class, we can configure the form from the template. Angular will understand what we’re trying to do based on the elements and attributes we have in the template.
Accessing the objects
One of the main challenges of using template forms is that they don’t offer the same flexibility as reactive forms. This is because of how limited we are to what we can access. There is a way to grab the form groups and form controls that Angular generates.
In the app.component.html
template file, we’ll update the <form>
element to the following:
Get hands-on with 1400+ tech skills courses.