fix(forms): default the initial value of Control to null

This commit is contained in:
vsavkin 2015-07-16 18:34:03 -07:00
parent 4d28167bc0
commit 5b597de18c
4 changed files with 9 additions and 3 deletions

View File

@ -74,7 +74,7 @@ export class NgForm extends ControlContainer implements Form {
addControl(dir: NgControl): void { addControl(dir: NgControl): void {
this._later(_ => { this._later(_ => {
var container = this._findContainer(dir.path); var container = this._findContainer(dir.path);
var c = new Control(""); var c = new Control();
setUpControl(c, dir); setUpControl(c, dir);
container.addControl(dir.name, c); container.addControl(dir.name, c);
c.updateValidity(); c.updateValidity();

View File

@ -36,7 +36,7 @@ const formControlBinding = CONST_EXPR(new Binding(NgControl, {toAlias: forwardRe
exportAs: 'form' exportAs: 'form'
}) })
export class NgModel extends NgControl { export class NgModel extends NgControl {
_control = new Control(""); _control = new Control();
_added = false; _added = false;
update = new EventEmitter(); update = new EventEmitter();
model: any; model: any;

View File

@ -144,7 +144,7 @@ export class AbstractControl {
export class Control extends AbstractControl { export class Control extends AbstractControl {
_onChange: Function; _onChange: Function;
constructor(value: any, validator: Function = Validators.nullValidator) { constructor(value: any = null, validator: Function = Validators.nullValidator) {
super(validator); super(validator);
this._value = value; this._value = value;
this.updateValidity({onlySelf: true}); this.updateValidity({onlySelf: true});

View File

@ -19,6 +19,12 @@ import {ObservableWrapper} from 'angular2/src/facade/async';
export function main() { export function main() {
describe("Form Model", () => { describe("Form Model", () => {
describe("Control", () => { describe("Control", () => {
it("should default the value to null", () => {
var c = new Control();
expect(c.value).toBe(null);
expect(c.validator).toBe(Validators.nullValidator);
});
describe("validator", () => { describe("validator", () => {
it("should run validator with the initial value", () => { it("should run validator with the initial value", () => {
var c = new Control("value", Validators.required); var c = new Control("value", Validators.required);