More Related Content Similar to Angular 2.0 forms (20) More from Eyal Vardi (20) Angular 2.0 forms3. Form Directives (Template Driven)
'angular2/common'
import { FORM_DIRECTIVES } from 'angular2/common';
ngForm
ngFormModel
ngModel
ngControl
ngFormControl
Controls (Model Driven)
Validators
Accessors
Control
ControlGroup
ControlArray
10. Component
Field
Component Template Directive that in
use
name : string <input [(ngModel)]=“name” /> ngModel
name : string <input ngControl=“dName” [(ngModel)]=“name” /> ngControlName
'[ngControl]'
'[ngModel]:not([ngControl]):not([ngFormControl])'
This is still a
ngControl
12. State Class if true Class if false
Control has been visited ng-touched ng-untouched
Control's value has changed ng-dirty ng-pristine
Control's value is valid ng-valid ng-invalid
13. @Directive({
selector: '[ngControl],[ngModel],[ngFormControl]',
host: {
'[class.ng-untouched]': 'ngClassUntouched',
'[class.ng-touched]' : 'ngClassTouched',
'[class.ng-pristine]' : 'ngClassPristine',
'[class.ng-dirty]' : 'ngClassDirty',
'[class.ng-valid]' : 'ngClassValid',
'[class.ng-invalid]' : 'ngClassInvalid'
}
})export class NgControlStatus {
private _cd: NgControl;
constructor(@Self() cd: NgControl) { this._cd = cd; }
...
get ngClassValid(): boolean {
return isPresent(this._cd.control) ? this._cd.control.valid : false;
}
...
}
15. selector : '[ngModel]:not([ngControl]):not([ngFormControl])',
Inputs : ['model: ngModel'],
outputs : ['update: ngModelChange'],
selector : '[ngFormControl]'
inputs : ['form: ngFormControl', 'model: ngModel'],
outputs : ['update: ngModelChange'],
selector : '[ngControl]',
inputs : ['name: ngControl', 'model: ngModel'],
outputs : ['update: ngModelChange'],
18. <div *ngIf="!myForm.valid" class="ui error message">
Form is invalid
</div>
<div *ngIf="!name.valid" class="ui message"
[class.error]="!name.valid && name.touched" >
Name is invalid
</div>
<div *ngIf="name.hasError('required')"
class="ui error message">Name is required</div>
23. export class App {
constructor() {
this.myForm = new ControlGroup({
myControl: new Control("")
});
}
}
<form [ngFormModel]="myForm">
<input ngFormControl="myControl">
</form>
25. @Component({
selector: 'my-app',
viewBindings: [FORM_BINDINGS],
template: `
<form [ngFormModel]="loginForm">
<p>Login <input ngFormControl="login"></p>
<div ngControlGroup="passwordRetry">
<p>Password
<input type="password"
ngFormControl="password"></p>
<p>Confirm password
<input type="password"
ngFormControl="pConfirma"></p>
</div>
</form>
<h3>Form value:</h3>
<pre>{{value}}</pre>
`,
directives: [FORM_DIRECTIVES]
})
export class App {
loginForm: ControlGroup;
constructor(builder: FormBuilder) {
this.loginForm = builder.group({
login: ["", Validators.required],
passwordRetry: builder.group({
password: ["", Validators.required],
pConfirm: ["", Validators.required]
})
});
}
get value(): string {
return JSON.stringify(
this.loginForm.value, null, 2);
}
}
34. @Directive({
selector:
'input:not([type=checkbox])[ngControl],
textarea[ngControl],
input:not([type=checkbox])[ngFormControl],
textarea[ngFormControl],
input:not([type=checkbox])[ngModel],
textarea[ngModel],
[ngDefaultControl]',
host: {
'(input)': 'onChange($event.target.value)',
'(blur)' : 'onTouched()'},
bindings: [DEFAULT_VALUE_ACCESSOR]
}) export class DefaultValueAccessor implements ControlValueAccessor {
onChange = (_) => {};
onTouched = () => {};
constructor(private _renderer:Renderer, private _elementRef:ElementRef) {}
writeValue(value: any): void {
var normalizedValue = isBlank(value) ? '' : value;
this._renderer.setElementProperty(
this._elementRef, 'value', normalizedValue);
}
registerOnChange(fn: (_: any) => void): void { this.onChange = fn; }
registerOnTouched(fn: () => void): void { this.onTouched = fn; }
}
35. export function setUpControl( control: Control , dir: NgControl ): void {
...
dir.valueAccessor.writeValue(control.value);
// view -> model
dir.valueAccessor.registerOnChange(newValue => {
dir.viewToModelUpdate(newValue);
control.updateValue(newValue, {emitModelToViewChange: false});
control.markAsDirty();
});
// model -> view
control.registerOnChange(newValue => dir.valueAccessor.writeValue(newValue));
// touched
dir.valueAccessor.registerOnTouched(() => control.markAsTouched());
}