Angular 2 Forms Cheat Sheet
by Nathan (Nathane2005) via [Link]/28056/cs/8477/
Introduction Angular 2 Form - Elements (cont) Displaying Validator Failures
A form creates a cohesive, effective, and FormArray Tracks the value and validity <label for="name">Name</label>
compelling data entry experience. An state of an array of FormCo‐ <input type="t‐
Angular form coordinates a set of data- ntrol instances. A FormArray ext" class="form-cont‐
bound user controls, tracks changes, aggregates the values of each rol" id="name"
validates input, and presents errors. child FormControl into an ‐
array required
Form FormBu‐ Creates an AbstractControl ‐
<form> ilder from a user-specified config‐ [(ngModel)]="model.name"
.... tags that include all uration. It is essentially name="name"
input elements syntactic sugar that shortens ‐
</form> the new FormGroup(), new
#name="ngModel" >
FormControl(), and new
All forms are placed within the HTML form <div [hidde‐
FormArray() boilerplate that
tags n]="nam[Link] || name.p‐
can build up in larger form
ristine"
Requires use of FormModule
Standard Input Types cla‐
ss="alert alert-danger">
Text Input <input type="text">
Reactive Form Names
Name is
Email Input <input type="email">
formGr‐ Used to reference a group of required
Password <input type="password"> oupName elements </div>
Input
formCo‐ Similar to ngModel reference to
Dropdown <select> ntr‐ a name but simpler from a Workflow
Selection <option value="volvo">Vol‐ olName naming convention perspective Steps to creating a reactive form:
vo</option>
formAr‐ Syncs a nested FormArray to a 1. Create the Domain Model
<option value="saab">S‐
rayName DOM element. 2. Create the Controller with references to
aab</option>
Requires the use of the ReactiveForm‐ View
<option value="opel">Opel‐
sModule Module 3. Create the View
</option>
4. Add Validations
<option
Handling Submission Event 5. Add Submit Validation Control
value="audi">Audi</option>
6. Add Dynamic Behaviors
</select> <form (ngSubmit)="onSubmit()">
Multi <select multiple> ...
Model
Selection <option value="volvo">Vol‐ </form>
export interface {ModelName} {
vo</option>
item(? : optional) :
<option value="saab">S‐ Standard Validation
aab</option> string | number | date | boolean
Mandatory Validators.required
<option value="opel">Opel‐ | class | interface ([] :
Minimum Valida[Link]nLengt‐ array);
</option>
Length h(size)
<option }
value="audi">Audi</option> Maximum Validators.maxLength(‐
</select> Length size) Controller
Checkbox <input type="checkbox"> Pattern Match Validators.pattern("re‐
gEx")
Radio <input type="radio">
Control
Custom Validators
Numeric <input type="number">
Input
Date <input type="date">
Multiline <textarea rows="4" cols="5‐
Input 0"></textarea>
Angular 2 Form - Elements
FormGroup A FormGroup aggregates the function {name}(control : let style =
values of each child FormCo‐ FormControl) : {[s: string] : require('./[Link]');
ntrol into one object, with boolean} { let template = require("./so‐
each control name as the key meTemplate.html");
FormCo‐ Tracks the value and .... function body.... @Component({
ntrol validation status of an pass return a null styles:[style],
individual form control. It is fail return an object of type template: template
one of the three fundamental {key : true} });
building blocks of Angular } export class {Some}Form
forms implements OnInit{
myForm: FormGroup;
constructor(private fb :
FormBuilder) {};
ngOnInit() {
//Construct the form
data type
this.myForm: this.f‐
b.group({
'controlName' :
this.fb.control(...),
'controlArray'
: this.fb.array([...]),
'controlGroup'
: this.fb.group({})
});
}
onSubmit() {
myF[Link]; //returns
the form values
By Nathan (Nathane2005) Published 9th October, 2016. Sponsored by [Link]
Last updated 9th October, 2016. Measure your website readability!
Page 1 of 3. [Link]
[Link]/nathane2005/
Angular 2 Forms Cheat Sheet
by Nathan (Nathane2005) via [Link]/28056/cs/8477/
Controller (cont)
> myModel = <MyModel>myF[Link]‐
lue;//Cast to object
}
}
Typical additions include:
1. Http Service Submission (delegate
normally injected)
2. Pipes for Display customization
3. Model based Validators
View
<form [formGroup]='myForm'
(ngSubmit)='onSubmit()'>
<input formControlName‐
=''>
<div formGroupName=''>
<input formContr‐
olName=''>
</div>
<div formArrayName=''>
<input
formControlName='{{index}}'
*ngFor='let
item of items; index = index'>
</div>
</form>
Useful Blocks
-- Get Form Items
JSON.stringify(myF‐
[Link])
Useful Links
Angular Forms
TypeScript Basic Types
HTML Inputs
By Nathan (Nathane2005) Published 9th October, 2016. Sponsored by [Link]
Last updated 9th October, 2016. Measure your website readability!
Page 2 of 3. [Link]
[Link]/nathane2005/