Skip to content

Commit b02bd65

Browse files
committed
feat(forms): made forms works with single controls
1 parent ee36aaf commit b02bd65

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

modules/angular2/src/forms/directives.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,20 @@ export class CheckboxControlValueAccessor {
5959
lifecycle: [onChange],
6060
selector: '[control]',
6161
bind: {
62-
'controlName' : 'control'
62+
'controlOrName' : 'control'
6363
}
6464
})
6565
export class ControlDirective {
6666
_groupDirective:ControlGroupDirective;
6767

68-
controlName:string;
68+
controlOrName:any;
6969
valueAccessor:any; //ControlValueAccessor
7070

7171
validator:Function;
7272

73-
constructor(@Ancestor() groupDirective:ControlGroupDirective, valueAccessor:DefaultValueAccessor) {
73+
constructor(@Optional() @Ancestor() groupDirective:ControlGroupDirective, valueAccessor:DefaultValueAccessor) {
7474
this._groupDirective = groupDirective;
75-
this.controlName = null;
75+
this.controlOrName = null;
7676
this.valueAccessor = valueAccessor;
7777
this.validator = Validators.nullValidator;
7878
}
@@ -84,7 +84,9 @@ export class ControlDirective {
8484
}
8585

8686
_initialize() {
87-
this._groupDirective.addDirective(this);
87+
if(isPresent(this._groupDirective)) {
88+
this._groupDirective.addDirective(this);
89+
}
8890

8991
var c = this._control();
9092
c.validator = Validators.compose([c.validator, this.validator]);
@@ -102,7 +104,11 @@ export class ControlDirective {
102104
}
103105

104106
_control() {
105-
return this._groupDirective.findControl(this.controlName);
107+
if (isString(this.controlOrName)) {
108+
return this._groupDirective.findControl(this.controlOrName);
109+
} else {
110+
return this.controlOrName;
111+
}
106112
}
107113
}
108114

modules/angular2/test/forms/integration_spec.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,24 @@ export function main() {
111111
});
112112
}));
113113

114+
it("should work with single controls", inject([AsyncTestCompleter], (async) => {
115+
var control = new Control("loginValue");
116+
var ctx = new MyComp(control);
117+
118+
var t = `<div><input type="text" [control]="form"></div>`;
119+
120+
compile(MyComp, t, ctx, (view) => {
121+
var input = queryView(view, "input")
122+
expect(input.value).toEqual("loginValue");
123+
124+
input.value = "updatedValue";
125+
dispatchEvent(input, "change");
126+
127+
expect(control.value).toEqual("updatedValue");
128+
async.done();
129+
});
130+
}));
131+
114132
it("should update DOM elements when rebinding the control group", inject([AsyncTestCompleter], (async) => {
115133
var form = new ControlGroup({
116134
"login": new Control("oldValue")
@@ -376,7 +394,7 @@ export function main() {
376394
selector: "my-comp"
377395
})
378396
class MyComp {
379-
form:ControlGroup;
397+
form:any;
380398
name:string;
381399

382400
constructor(form = null, name = null) {

0 commit comments

Comments
 (0)