Now, you need to connect your form to the template using the [field] directive. This creates two-way data binding between your form model and the input elements.
In this lesson, you'll learn how to:
- Import the
Fielddirective - Use the
[field]directive to bind form fields to inputs - Connect text inputs and checkboxes to your form
- Display form field values in the template
Let's wire up the template!
-
Import the Field directive
Import the
Fielddirective from@angular/forms/signalsand add it to your component's imports array: -
Bind the email field
In your template, add the
[field]directive to the email input:The
loginForm.emailexpression accesses the email field from your form. -
Bind the password field
Add the
[field]directive to the password input: -
Bind the checkbox field
Add the
[field]directive to the checkbox input: -
Display the form values
Below the form, there's a debug section to show current form values. Display each field value using
.value():<p>Email: {{ loginForm.email().value() }}</p><p>Password: {{ loginForm.password().value() ? '••••••••' : '(empty)' }}</p><p>Remember me: {{ loginForm.rememberMe().value() ? 'Yes' : 'No' }}</p>Form field values are signals, so the displayed values update automatically as you type.
Great work! You've connected your form to the template and displayed the form values. The [field] directive handles two-way data binding automatically - as you type, the loginModel signal updates, and the displayed values update immediately.
Next, you'll learn how to add validation to your form!