Forms
import { FormsModule } from '@angular/forms'
@NgModule({
imports: [
...,
FormsModule
],
declarations: [
...,
],
bootstrap: [
...,
]
})
export class AppModule { }
component
ng2 controls
Component view
- pristine
- dirty
- touched
- untouched
- errors
- valid
@Component({
selector: "my-form",
template: `<input type="text" [(ngModel)]="user.name">`
})
class MyForm {
user: User = new User('name')
}
@Component({
selector: "my-form",
template: `
<input type="text" [(ngModel)]="user.name" #field="ngModel">
<pre>
{{ field.valid }}
</pre>
`
})
class MyForm {
user: User = new User('name')
}
<form [formGroup]="loginForm">
<p>Login <input formControlName="login"></p>
<div formGroupName="passwordRetry">
<p>Password <input type="password" formControlName="password"></p>
<p>Confirm password <input type="password" formControlName="passwordConfirmation"></p>
</div>
</form>
import { Component } from '@angular/core'
import { REACTIVE_FORM_DIRECTIVES, FormBuilder, FormGroup, Validators } from '@angular/forms'
@Component({
selector: 'nat-login',
template: require('./login.component.html'),
directives: [ REACTIVE_FORM_DIRECTIVES ]
})
export class LoginComponent {
loginForm: FormGroup
constructor(private _formBuilder: FormBuilder) {
this.loginForm = _formBuilder.group({
email: ["", Validators.required],
passwordRetry: _formBuilder.group({
password: ["", Validators.required],
passwordConfirmation: ["", Validators.required, asyncValidator]
})
})
}
}
function containsTroll(c: Control) {
if(c.value.indexOf('troll') >= 0) {
return {
noTroll: true
}
}
return null
}
this.loginForm = _formBuilder.group({
email: ['', containsTroll]
password: ['', Validators.required],
});
// Component
this.name = new Control('', Validators.minLength(4));
// View
<input required type="text" formControlName="name" />
<div [hidden]="name.dirty && !name.valid">
<p [hidden]="name.errors.minlength">
Your name needs to be at least 4 characters.
</p>
</div>
interface ValidationResult {
[key:string]:boolean;
}
// Component
const containsTroll: ValidatorFn = (c: AbstractControl) => {
if(c.value.indexOf('troll') >= 0) {
return {
noTroll: true
}
}
return null
}
// ...
name: ["", Validators.required, containsTroll],
// ...
// View
<input required type="text" formControlName="name" />
<div [hidden]="name.dirty && !name.valid">
<p [hidden]="name.errors.noTroll">
There is "troll" in your name ...
</p>
</div>
// Component
function exists(c: Control): Promise<ValidationResult> {
let q = new Promise((resolve, reject) => {
setTimeout(() => {
if (c.value === 'George') {
resolve({"usernameExists": true})
} else {
resolve(null)
}
}, 1000)
})
return q
}
email: ["", Validators.required, asyncName],
// View
<input required type="text" formControlName="name" />
<p [show]="name.pending">
Fetching data from server!
</p>
<div [hidden]="name.dirty && !name.valid && !name.pending">
<p [hidden]="name.errors.required">
Your name is required!
</p>
<p [hidden]="name.errors.usernameExists">
This username already exists! sad ;-(
</p>
</div>
let validators = Validators.compose(Validators.required, containsTroll)
let asyncValidators = Validators.composeAsync(exists, exists1)