64 lines
1.6 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 {forwardRef, Binding} from 'angular2/di';
2015-05-31 12:24:34 -07:00
import {NgControl} from './ng_control';
2015-05-31 12:24:34 -07:00
import {Control} from '../model';
import {setUpControl} from './shared';
const formControlBinding = CONST_EXPR(new Binding(NgControl, {toAlias: forwardRef(() => NgModel)}));
2015-05-31 12:24:34 -07:00
/**
* Binds a domain model to the form.
*
* # Example
* ```
* @Component({selector: "search-comp"})
* @View({
* directives: [formDirectives],
* template: `
<input type='text' [(ng-model)]="searchQuery">
* `})
* class SearchComp {
* searchQuery: string;
* }
* ```
*
* @exportedAs angular2/forms
*/
2015-05-31 12:24:34 -07:00
@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 NgModel extends NgControl {
2015-06-16 15:53:52 -07:00
_control = new Control("");
_added = false;
ngModel = new EventEmitter();
2015-05-31 12:24:34 -07:00
model: any;
onChange(c) {
if (!this._added) {
2015-06-16 15:53:52 -07:00
setUpControl(this._control, this);
2015-05-31 12:24:34 -07:00
this.control.updateValidity();
this._added = true;
};
if (StringMapWrapper.contains(c, "model")) {
2015-06-16 15:53:52 -07:00
this._control.updateValue(this.model);
2015-05-31 12:24:34 -07:00
}
}
2015-06-16 15:53:52 -07:00
get control() { return this._control; }
2015-05-31 12:24:34 -07:00
get path(): List<string> { return []; }
viewToModelUpdate(newValue: any): void { ObservableWrapper.callNext(this.ngModel, newValue); }
}