diff --git a/modules/angular2/src/core/forms/directives/abstract_control_directive.ts b/modules/angular2/src/core/forms/directives/abstract_control_directive.ts index aef8e72217..9a881f10d4 100644 --- a/modules/angular2/src/core/forms/directives/abstract_control_directive.ts +++ b/modules/angular2/src/core/forms/directives/abstract_control_directive.ts @@ -1,19 +1,22 @@ import {AbstractControl} from '../model'; +import {isPresent} from 'angular2/src/core/facade/lang'; export class AbstractControlDirective { get control(): AbstractControl { return null; } - get value(): any { return this.control.value; } + get value(): any { return isPresent(this.control) ? this.control.value : null; } - get valid(): boolean { return this.control.valid; } + get valid(): boolean { return isPresent(this.control) ? this.control.valid : null; } - get errors(): StringMap { return this.control.errors; } + get errors(): StringMap { + return isPresent(this.control) ? this.control.errors : null; + } - get pristine(): boolean { return this.control.pristine; } + get pristine(): boolean { return isPresent(this.control) ? this.control.pristine : null; } - get dirty(): boolean { return this.control.dirty; } + get dirty(): boolean { return isPresent(this.control) ? this.control.dirty : null; } - get touched(): boolean { return this.control.touched; } + get touched(): boolean { return isPresent(this.control) ? this.control.touched : null; } - get untouched(): boolean { return this.control.untouched; } + get untouched(): boolean { return isPresent(this.control) ? this.control.untouched : null; } } \ No newline at end of file diff --git a/modules/angular2/test/core/forms/integration_spec.ts b/modules/angular2/test/core/forms/integration_spec.ts index 097dda1198..a7d1e92e8b 100644 --- a/modules/angular2/test/core/forms/integration_spec.ts +++ b/modules/angular2/test/core/forms/integration_spec.ts @@ -800,6 +800,19 @@ export function main() { rootTC.detectChanges(); expect(input.value).toEqual("aa"); }))); + it("should not crash when validity is checked from a binding", + inject([TestComponentBuilder], fakeAsync((tcb: TestComponentBuilder) => { + // {{x.valid}} used to crash because valid() tried to read a property + // from form.control before it was set. This test verifies this bug is + // fixed. + var t = `
+
{{x.valid}}
`; + var rootTC: RootTestComponent; + tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then( + (root) => { rootTC = root; }); + tick(); + rootTC.detectChanges(); + }))); }); }); }