angular-cn/modules/angular2/src/forms/directives/ng_model_directive.ts

52 lines
1.4 KiB
TypeScript
Raw Normal View History

2015-05-31 12:24:34 -07:00
import {CONST_EXPR} from 'angular2/src/facade/lang';
import {EventEmitter, ObservableWrapper} from 'angular2/src/facade/async';
import {StringMapWrapper} from 'angular2/src/facade/collection';
import {Directive, Ancestor, onChange} from 'angular2/angular2';
import {FORWARD_REF, Binding} from 'angular2/di';
import {ControlDirective} from './control_directive';
import {Control} from '../model';
import {setUpControl} from './shared';
const formControlBinding =
CONST_EXPR(new Binding(ControlDirective, {toAlias: FORWARD_REF(() => NgModelDirective)}));
@Directive({
selector: '[ng-model]:not([ng-control]):not([ng-form-control])',
2015-05-31 12:24:34 -07:00
hostInjector: [formControlBinding],
properties: ['model: ng-model'],
events: ['ngModel'],
lifecycle: [onChange],
exportAs: 'form'
2015-05-31 12:24:34 -07:00
})
export class NgModelDirective extends ControlDirective {
control: Control;
ngModel: EventEmitter;
model: any;
_added: boolean;
constructor() {
super();
this.control = new Control("");
this.ngModel = new EventEmitter();
this._added = false;
}
onChange(c) {
if (!this._added) {
setUpControl(this.control, this);
this.control.updateValidity();
this._added = true;
};
if (StringMapWrapper.contains(c, "model")) {
this.control.updateValue(this.model);
2015-05-31 12:24:34 -07:00
}
}
get path(): List<string> { return []; }
viewToModelUpdate(newValue: any): void { ObservableWrapper.callNext(this.ngModel, newValue); }
}