@@ -32,35 +32,6 @@ function _lookupControl(groupDirective: ControlGroupDirective, controlOrName: an
3232 return control ;
3333}
3434
35-
36- /**
37- * The default accessor for writing a value and listening to changes that is used by a {@link
38- * Control} directive.
39- *
40- * This is the default strategy that Angular uses when no other accessor is applied.
41- *
42- * # Example
43- * ```
44- * <input type="text" [control]="loginControl">
45- * ```
46- *
47- * @exportedAs angular2/forms
48- */
49- @Directive ( {
50- selector : '[control]' ,
51- hostListeners :
52- { 'change' : 'onChange($event.target.value)' , 'input' : 'onChange($event.target.value)' } ,
53- hostProperties : { 'value' : 'value' }
54- } )
55- export class DefaultValueAccessor {
56- value ;
57- onChange : Function ;
58-
59- constructor ( ) { this . onChange = ( _ ) => { } ; }
60-
61- writeValue ( value ) { this . value = value }
62- }
63-
6435/**
6536 * Binds a control group to a DOM element.
6637 *
@@ -171,11 +142,9 @@ export class ControlDirective {
171142
172143 validator : Function ;
173144
174- constructor ( @Optional ( ) @Ancestor ( ) groupDirective : ControlGroupDirective ,
175- valueAccessor : DefaultValueAccessor ) {
145+ constructor ( @Optional ( ) @Ancestor ( ) groupDirective : ControlGroupDirective ) {
176146 this . _groupDirective = groupDirective ;
177147 this . _controlOrName = null ;
178- this . valueAccessor = valueAccessor ;
179148 this . validator = Validators . nullValidator ;
180149 }
181150
@@ -189,6 +158,10 @@ export class ControlDirective {
189158 var c = this . _control ( ) ;
190159 c . validator = Validators . compose ( [ c . validator , this . validator ] ) ;
191160
161+ if ( isBlank ( this . valueAccessor ) ) {
162+ throw new BaseException ( `Cannot find value accessor for control "${ controlOrName } "` ) ;
163+ }
164+
192165 this . _updateDomValue ( ) ;
193166 this . _setUpUpdateControlValue ( ) ;
194167 }
@@ -202,6 +175,75 @@ export class ControlDirective {
202175 _control ( ) { return _lookupControl ( this . _groupDirective , this . _controlOrName ) ; }
203176}
204177
178+ /**
179+ * The default accessor for writing a value and listening to changes that is used by a {@link
180+ * Control} directive.
181+ *
182+ * This is the default strategy that Angular uses when no other accessor is applied.
183+ *
184+ * # Example
185+ * ```
186+ * <input type="text" [control]="loginControl">
187+ * ```
188+ *
189+ * @exportedAs angular2/forms
190+ */
191+ @Directive ( {
192+ selector : 'input:not([type=checkbox])[control],textarea[control]' ,
193+ hostListeners :
194+ { 'change' : 'onChange($event.target.value)' , 'input' : 'onChange($event.target.value)' } ,
195+ hostProperties : { 'value' : 'value' }
196+ } )
197+ export class DefaultValueAccessor {
198+ value = null ;
199+ onChange : Function ;
200+
201+ constructor ( cd : ControlDirective , private _elementRef : ElementRef , private _renderer : Renderer ) {
202+ this . onChange = ( _ ) => { } ;
203+ cd . valueAccessor = this ;
204+ }
205+
206+ writeValue ( value ) {
207+ this . _renderer . setElementProperty ( this . _elementRef . parentView . render ,
208+ this . _elementRef . boundElementIndex , 'value' , value )
209+ }
210+ }
211+
212+ /**
213+ * The accessor for writing a value and listening to changes that is used by a {@link
214+ * Control} directive.
215+ *
216+ * This is the default strategy that Angular uses when no other accessor is applied.
217+ *
218+ * # Example
219+ * ```
220+ * <input type="text" [control]="loginControl">
221+ * ```
222+ *
223+ * @exportedAs angular2/forms
224+ */
225+ @Directive ( {
226+ selector : 'select[control]' ,
227+ hostListeners :
228+ { 'change' : 'onChange($event.target.value)' , 'input' : 'onChange($event.target.value)' } ,
229+ hostProperties : { 'value' : 'value' }
230+ } )
231+ export class SelectControlValueAccessor {
232+ value = null ;
233+ onChange : Function ;
234+
235+ constructor ( cd : ControlDirective , private _elementRef : ElementRef , private _renderer : Renderer ) {
236+ this . onChange = ( _ ) => { } ;
237+ this . value = '' ;
238+ cd . valueAccessor = this ;
239+ }
240+
241+ writeValue ( value ) {
242+ this . _renderer . setElementProperty ( this . _elementRef . parentView . render ,
243+ this . _elementRef . boundElementIndex , 'value' , value )
244+ }
245+ }
246+
205247/**
206248 * The accessor for writing a value and listening to changes on a checkbox input element.
207249 *
@@ -219,17 +261,12 @@ export class ControlDirective {
219261 hostProperties : { 'checked' : 'checked' }
220262} )
221263export class CheckboxControlValueAccessor {
222- _elementRef : ElementRef ;
223- _renderer : Renderer ;
224-
225264 checked : boolean ;
226265 onChange : Function ;
227266
228- constructor ( cd : ControlDirective , elementRef : ElementRef , renderer : Renderer ) {
267+ constructor ( cd : ControlDirective , private _elementRef : ElementRef , private _renderer : Renderer ) {
229268 this . onChange = ( _ ) => { } ;
230- this . _elementRef = elementRef ;
231- this . _renderer = renderer ;
232- cd . valueAccessor = this ; // ControlDirective should inject CheckboxControlDirective
269+ cd . valueAccessor = this ;
233270 }
234271
235272 writeValue ( value ) {
@@ -246,5 +283,10 @@ export class CheckboxControlValueAccessor {
246283 *
247284 * @exportedAs angular2/forms
248285 */
249- export const formDirectives : List < Type > = CONST_EXPR (
250- [ ControlGroupDirective , ControlDirective , CheckboxControlValueAccessor , DefaultValueAccessor ] ) ;
286+ export const formDirectives : List < Type > = CONST_EXPR ( [
287+ ControlGroupDirective ,
288+ ControlDirective ,
289+ CheckboxControlValueAccessor ,
290+ DefaultValueAccessor ,
291+ SelectControlValueAccessor
292+ ] ) ;
0 commit comments