这是代码
@Component({
selector: 'elastic-textarea',
template:
`
<ion-textarea #textarea
placeholder='{{placeholder}}'
[(ngModel)]="content"
(ngModelChange)='onChange($event)'></ion-textarea>
`
})
export class ElasticTextarea {
@ViewChild('textarea') ionTxtArea: ElementRef;
etc...
txtArea: any;
constructor() {
...
}
ngAfterViewInit(){
this.txtArea = this.ionTxtArea.nativeElement; <- This is null
this.txtArea.style.height = this.lineHeight + "px"; <- Crashes here
}
似乎我正在做的一切正确。但是,出于某种原因,我无法获得该textarea元素的引用,以便进行一些DOM操作... 发生了什么事?
答案 0 :(得分:0)
devqon评论是对的。但是,如果您想要这样做,则应更改ViewChild
以避免打字稿错误:
@ViewChild('textarea') ionTxtArea: any;
ngAfterViewInit(){
//Just log your ionTtxtArea here to see what is it:
console.log(this.ionTxtArea);
this.txtArea = this.ionTxtArea._elementRef.nativeElement.children[0];
this.txtArea.style.height = this.lineHeight + "px";
}