angular - 無法從 Ionic2 的 ElementRef.nativeElement 獲取屬性
首先,我要解釋我想做的事情。我在一個表單中有一個 ion-textarea,我想根據內容調整高度。我對 ionic2 仍然是新手,我想正確地做事並學習,所以我認為最好的方法是創建一個指令,然後在 ion-textarea 中使用它。
基本上我需要的是:- 從 textarea 獲取 scrollHeight - 將 textarea 的高度設置為 scrollHeight 值
我知道如何使用 Renderer 設定高度值,我的問題是如何獲取 scrollHeight(或其他任何屬性值)。
這是我的測試代碼:
import {Directive, ElementRef, Renderer} from '@angular/core';
@Directive({
selector: '[textarea-autosize]',
})
export class TextareaAutosize{
private el: HTMLElement;
constructor(private element:ElementRef, private render:Renderer){
}
ngAfterViewInit(){
this.el = this.element.nativeElement.querySelector('textarea'); //I have checked without querySelector too
console.log(this.element.nativeElement.style); //this return CSSStyleDeclaration with all empty properties
console.log(this.el.scrollHeight); //this return always 0
console.log(this.el); //this return the html textarea code
this.render.setElementStyle(this.el, 'height', 100 + 'px'); //this set height to 100px
}
}
我上網搜尋了很多,發現有人獲得的屬性和我一樣,因此,我做錯了什麼,或者也許我想要的必須以另一種方式來達成。