Angular Can´;无法从Ionic2中的ElementRef.nativeElement获取属性
首先我要解释我想做什么。我在表单中有一个Angular Can´;无法从Ionic2中的ElementRef.nativeElement获取属性,angular,typescript,ionic2,Angular,Typescript,Ionic2,首先我要解释我想做什么。我在表单中有一个ion文本区域,我想根据内容调整高度。我还是一个ionic2的新手,我正在努力做正确的事情并学习,所以我认为最好的方法是创建一个指令,然后在ion textarea中使用它 基本上我需要的是: -从textarea -将textareaheight设置为scrollHeight值 我知道如何使用渲染器设置高度值,我的问题是获取scrollHeight(或任何其他属性值) 这是我的测试代码: import {Directive, ElementRef, Re
ion文本区域
,我想根据内容调整高度。我还是一个ionic2的新手,我正在努力做正确的事情并学习,所以我认为最好的方法是创建一个指令,然后在ion textarea
中使用它
基本上我需要的是:
-从textarea
-将textarea
height设置为scrollHeight
值
我知道如何使用渲染器设置高度值,我的问题是获取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
}
}
我在谷歌上搜索了很多,我发现人们都像我一样有属性,因此,我做错了什么,或者我想用另一种方式来做什么