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
}
}
我搜索了很多,发现有人获得的属性和我一样,因此,我可能做错了什么,或者我想要得到的东西可能需要通过另一种方式来实现。