From 05ca4a7bec66879696ac4a1df73390b32b32526a Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Fri, 28 Jul 2017 17:47:45 +0300 Subject: [PATCH] fix(textarea): server-side rendering error when using mdTextareaAutosize (#6050) Fixes a server-side rendering error when using the `mdTextareaAutosize` directive. Fixes #6047. --- src/lib/input/autosize.ts | 14 +++++++++++--- src/universal-app/kitchen-sink/kitchen-sink.html | 3 +++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/lib/input/autosize.ts b/src/lib/input/autosize.ts index 796b03ca0ce7..70d6afe22f7f 100644 --- a/src/lib/input/autosize.ts +++ b/src/lib/input/autosize.ts @@ -8,6 +8,7 @@ import {Directive, ElementRef, Input, AfterViewInit, Optional, Self} from '@angular/core'; import {NgControl} from '@angular/forms'; +import {Platform} from '@angular/cdk/platform'; /** @@ -57,7 +58,11 @@ export class MdTextareaAutosize implements AfterViewInit { /** Cached height of a textarea with a single row. */ private _cachedLineHeight: number; - constructor(private _elementRef: ElementRef, @Optional() @Self() formControl: NgControl) { + constructor( + private _elementRef: ElementRef, + private _platform: Platform, + @Optional() @Self() formControl: NgControl) { + if (formControl && formControl.valueChanges) { formControl.valueChanges.subscribe(() => this.resizeToFitContent()); } @@ -84,8 +89,10 @@ export class MdTextareaAutosize implements AfterViewInit { } ngAfterViewInit() { - this._cacheTextareaLineHeight(); - this.resizeToFitContent(); + if (this._platform.isBrowser) { + this._cacheTextareaLineHeight(); + this.resizeToFitContent(); + } } /** Sets a style property on the textarea element. */ @@ -131,6 +138,7 @@ export class MdTextareaAutosize implements AfterViewInit { /** Resize the textarea to fit its content. */ resizeToFitContent() { const textarea = this._elementRef.nativeElement as HTMLTextAreaElement; + if (textarea.value === this._previousValue) { return; } diff --git a/src/universal-app/kitchen-sink/kitchen-sink.html b/src/universal-app/kitchen-sink/kitchen-sink.html index 28f85d48bdc2..f1ed4e7e2b5b 100644 --- a/src/universal-app/kitchen-sink/kitchen-sink.html +++ b/src/universal-app/kitchen-sink/kitchen-sink.html @@ -208,3 +208,6 @@

Toolbar

Tooltip

+ +

Autosize textarea

+