6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
9
- import { Directive , ElementRef , Input , AfterViewInit , Optional , Self } from '@angular/core' ;
10
- import { NgControl } from '@angular/forms' ;
9
+ import { Directive , ElementRef , Input , AfterViewInit , DoCheck } from '@angular/core' ;
11
10
import { Platform } from '@angular/cdk/platform' ;
12
11
13
12
@@ -19,13 +18,12 @@ import {Platform} from '@angular/cdk/platform';
19
18
textarea[mat-autosize], textarea[matTextareaAutosize]` ,
20
19
exportAs : 'mdTextareaAutosize' ,
21
20
host : {
22
- '(input)' : 'resizeToFitContent()' ,
23
21
// Textarea elements that have the directive applied should have a single row by default.
24
22
// Browsers normally show two rows by default and therefore this limits the minRows binding.
25
23
'rows' : '1' ,
26
24
} ,
27
25
} )
28
- export class MdTextareaAutosize implements AfterViewInit {
26
+ export class MdTextareaAutosize implements AfterViewInit , DoCheck {
29
27
/** Keep track of the previous textarea value to avoid resizing when the value hasn't changed. */
30
28
private _previousValue : string ;
31
29
@@ -58,15 +56,7 @@ export class MdTextareaAutosize implements AfterViewInit {
58
56
/** Cached height of a textarea with a single row. */
59
57
private _cachedLineHeight : number ;
60
58
61
- constructor (
62
- private _elementRef : ElementRef ,
63
- private _platform : Platform ,
64
- @Optional ( ) @Self ( ) formControl : NgControl ) {
65
-
66
- if ( formControl && formControl . valueChanges ) {
67
- formControl . valueChanges . subscribe ( ( ) => this . resizeToFitContent ( ) ) ;
68
- }
69
- }
59
+ constructor ( private _elementRef : ElementRef , private _platform : Platform ) { }
70
60
71
61
/** Sets the minimum height of the textarea as determined by minRows. */
72
62
_setMinHeight ( ) : void {
@@ -142,11 +132,17 @@ export class MdTextareaAutosize implements AfterViewInit {
142
132
this . _setMaxHeight ( ) ;
143
133
}
144
134
135
+ ngDoCheck ( ) {
136
+ this . resizeToFitContent ( ) ;
137
+ }
138
+
145
139
/** Resize the textarea to fit its content. */
146
140
resizeToFitContent ( ) {
147
141
const textarea = this . _elementRef . nativeElement as HTMLTextAreaElement ;
142
+ const value = textarea . value ;
148
143
149
- if ( textarea . value === this . _previousValue ) {
144
+ // Only resize of the value changed since these calculations can be expensive.
145
+ if ( value === this . _previousValue ) {
150
146
return ;
151
147
}
152
148
@@ -159,6 +155,6 @@ export class MdTextareaAutosize implements AfterViewInit {
159
155
textarea . style . height = `${ textarea . scrollHeight } px` ;
160
156
textarea . style . overflow = '' ;
161
157
162
- this . _previousValue = textarea . value ;
158
+ this . _previousValue = value ;
163
159
}
164
160
}
0 commit comments