66 * found in the LICENSE file at https://angular.io/license
77 */
88
9+ import { coerceBooleanProperty } from '@angular/cdk/coercion' ;
10+ import { getSupportedInputTypes , Platform } from '@angular/cdk/platform' ;
911import {
1012 Directive ,
1113 DoCheck ,
1214 ElementRef ,
15+ Inject ,
1316 Input ,
1417 OnChanges ,
1518 OnDestroy ,
1619 Optional ,
1720 Renderer2 ,
18- Self ,
21+ Self
1922} from '@angular/core' ;
20- import { coerceBooleanProperty } from '@angular/cdk/coercion' ;
21- import { FormGroupDirective , NgControl , NgForm , FormControl } from '@angular/forms' ;
22- import { Platform , getSupportedInputTypes } from '@angular/cdk/platform' ;
23- import { getMatInputUnsupportedTypeError } from './input-errors' ;
23+ import { FormControl , FormGroupDirective , NgControl , NgForm } from '@angular/forms' ;
2424import { ErrorStateMatcher } from '@angular/material/core' ;
25- import { Subject } from 'rxjs/Subject' ;
2625import { MatFormFieldControl } from '@angular/material/form-field' ;
26+ import { Subject } from 'rxjs/Subject' ;
27+ import { getMatInputUnsupportedTypeError } from './input-errors' ;
28+ import { MAT_INPUT_VALUE_ACCESSOR } from './input-value-accessor' ;
2729
2830// Invalid input type. Using one of these will throw an MatInputUnsupportedTypeError.
2931const MAT_INPUT_INVALID_TYPES = [
@@ -70,7 +72,7 @@ export class MatInput implements MatFormFieldControl<any>, OnChanges, OnDestroy,
7072 protected _required = false ;
7173 protected _id : string ;
7274 protected _uid = `mat-input-${ nextUniqueId ++ } ` ;
73- protected _previousNativeValue = this . value ;
75+ protected _previousNativeValue : any ;
7476 private _readonly = false ;
7577
7678 /** Whether the input is focused. */
@@ -129,10 +131,10 @@ export class MatInput implements MatFormFieldControl<any>, OnChanges, OnDestroy,
129131
130132 /** The input element's value. */
131133 @Input ( )
132- get value ( ) { return this . _elementRef . nativeElement . value ; }
133- set value ( value : string ) {
134+ get value ( ) : any { return this . _inputValueAccessor . value ; }
135+ set value ( value : any ) {
134136 if ( value !== this . value ) {
135- this . _elementRef . nativeElement . value = value ;
137+ this . _inputValueAccessor . value = value ;
136138 this . stateChanges . next ( ) ;
137139 }
138140 }
@@ -157,7 +159,14 @@ export class MatInput implements MatFormFieldControl<any>, OnChanges, OnDestroy,
157159 @Optional ( ) @Self ( ) public ngControl : NgControl ,
158160 @Optional ( ) protected _parentForm : NgForm ,
159161 @Optional ( ) protected _parentFormGroup : FormGroupDirective ,
160- private _defaultErrorStateMatcher : ErrorStateMatcher ) {
162+ private _defaultErrorStateMatcher : ErrorStateMatcher ,
163+ @Optional ( ) @Self ( ) @Inject ( MAT_INPUT_VALUE_ACCESSOR )
164+ private _inputValueAccessor : { value : any } ) {
165+ // If no input value accessor was explicitly specified, use the element as the input value
166+ // accessor.
167+ this . _inputValueAccessor = this . _inputValueAccessor || this . _elementRef . nativeElement ;
168+
169+ this . _previousNativeValue = this . value ;
161170
162171 // Force setter to be called in case id was not specified.
163172 this . id = this . id ;
0 commit comments