Because your app is SVC (single-view-component) β when you submit a form, how can web do it? β that's why angular need a special "things" for forms.
- Get input values by the users
- Check the valid of input
- Styling conditionally the form
- Template driven β inferred from DOM (html)
- Reactive β form created programmatically and synchronized with the DOM β more controls
π Codes for this section.
Everything you wanna do β do it in the template β template-driven!!!!
The form here don't make any HTML request. β
<form>
has no attribute!Angular doesn't recognize auto elements of
<form>
(label, input,...) because there may be some inputs which aren't served when submitting a form (their functions are different from a function of a form but they're inside <form>
) β ==need to tell angular which ones to be extra controlled?==π Codes for this section.
We can actually see what users entered.
Get the values user entered:
form.value
form.controls
β several properties to control the form.
form.dirty
:true
if we changed something in the form.
form.invalid
/form.valid
: if we add some invalidator / validator (to check the fields), this can be true or false.
- Read more in official doc.
π Codes for this section.
With
#f
(local ref), we can use @ViewChild
. β Useful when you wanna access the form not only at the time you submit it but also earlier!π Codes for this section.
There are 2 places for
.valid
information β form.valid
and form.controls.email.valid
When it's invalid (after clicking on submit) or valid β angular auto add classes to the html element β we can use these class to style our element!
Enable HTML5 validation (by default, Angular disables it) β
ngNativeValidate
π Codes for this section.
Using 1-way binding / property binding (
[ngModel]
) to set the default value.Before, the check only performed after clicking "Submit" β If you wanna check "lively" β Using two-way binding
[(NgModel)]
- 0-way binding,
NgModel
β tell angular that this input is a control
- 1-way binding,
[NgModel]
β get this control a default value
- 2-way binding,
[(NgModel)]
β Instantly out / do whatever you want with that value
In a big form, we need to "group" some values into a group / validate some specific group of inputs.
After submit: instead of getting
form.value.email
, but getting form.value.userData.email
(and also form.controls.userData
)π Codes for this section.
(video) With this HTML file
π Codes for this section.
Create the form programmatically (not from scratch).
π Codes for this section + Video.
π Codes for this section.
FormGroup
β a form, in the end, it's just a group of controls.
- We don't need
FormsModule
inapp.module.ts
(it's for template-driven form) β NEEDReactiveFormsModule
- We don't need local reference anymore.
- We configure all things in the typescript code (
component.ts
)
π Codes for this section.
FormGroup
inside FormGroup
.π Codes for this section.
Let the user dynamically add their form controls (we don't know yet there are how many controls there) β using an array of form.
Get access to
FormArray
π Codes for this section.
Get access directly to the FormControl using
.get()
π Codes for this section.
Suppose that we don't want users use some specific names.
π Codes for this section.
(video) Suppose we wanna check "already-taken" emails from the server.
.statusChanges
β gives the status of the form (INVALID, VALID, PENDING,...).valueChanges
β change something in the form (eg. typing something).Reset the form,