This cheat sheet has a quick overview of the important aspects of using Angular 2 Forms.
<form>
.... tags that include all input elements
</form>
|
Text Input
|
<input type="text">
|
|
Email Input
|
<input type="email">
|
|
Password Input
|
<input type="password">
|
|
Dropdown Selection
|
<select>
<option value="volvo">Volvo</option> <option value="saab">Saab</option> <option value="opel">Opel</option> <option value="audi">Audi</option> </select> |
|
Multi Selection
|
<select multiple>
<option value="volvo">Volvo</option> <option value="saab">Saab</option> <option value="opel">Opel</option> <option value="audi">Audi</option> </select> |
|
Checkbox
|
<input type="checkbox">
|
|
Radio Control
|
<input type="radio">
|
|
Numeric Input
|
<input type="number">
|
|
Date
|
<input type="date">
|
|
Multiline Input
|
<textarea rows="4" cols="50"></textarea>
|
|
FormGroup
|
A FormGroup aggregates the values of each child FormControl into one object, with each control name as the key
|
|
FormControl
|
Tracks the value and validation status of an individual form control. It is one of the three fundamental building blocks of Angular forms
|
|
FormArray
|
Tracks the value and validity state of an array of FormControl instances. A FormArray aggregates the values of each child FormControl into an array
|
|
FormBuilder
|
Creates an AbstractControl from a user-specified configuration. It is essentially syntactic sugar that shortens the new FormGroup(), new FormControl(), and new FormArray() boilerplate that can build up in larger form
|
|
formGroupName
|
Used to reference a group of elements
|
|
formControlName
|
Similar to ngModel reference to a name but simpler from a naming convention perspective
|
|
formArrayName
|
Syncs a nested FormArray to a DOM element.
|
<form (ngSubmit)="onSubmit()">
...
</form>
|
Mandatory
|
Validators.required
|
|
Minimum Length
|
Validator.minLength(size)
|
|
Maximum Length
|
Validators.maxLength(size)
|
|
Pattern Match
|
Validators.pattern("regEx")
|
function {name}(control : FormControl) : {[s: string] : boolean} {
.... function body....
pass return a null
fail return an object of type {key : true}
}
<label for="name">Name</label>
<input type="text" class="form-control" id="name"
required
[(ngModel)]="model.name" name="name"
#name="ngModel" >
<div [hidden]="name.valid || name.pristine"
class="alert alert-danger">
Name is required
</div>
export interface {ModelName} {
item(? : optional) : string | number | date | boolean | class | interface ([] : array);
}
let style = require('./someStyle.css');
let template = require("./someTemplate.html");
@Component({
styles:[style],
template: template
});
export class {Some}Form implements OnInit{
myForm: FormGroup;
constructor(private fb : FormBuilder) {};
ngOnInit() {
//Construct the form data type
this.myForm: this.fb.group({
'controlName' : this.fb.control(...),
'controlArray' : this.fb.array([...]),
'controlGroup' : this.fb.group({})
});
}
onSubmit() {
myForm.value; //returns the form values
myModel = <MyModel>myForm.value;//Cast to object
}
}
<form [formGroup]='myForm' (ngSubmit)='onSubmit()'>
<input formControlName=''>
<div formGroupName=''>
<input formControlName=''>
</div>
<div formArrayName=''>
<input formControlName='{{index}}'
*ngFor='let item of items; index = index'>
</div>
</form>
-- Get Form Items
JSON.stringify(myForm.value)
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.