我不明白爲什麼ViewChild
被返回null時完全相同的代碼工作的textarea
元素,但它不會在div
角2 ViewChild不起作用
模板工作:
<div #refId class = 'line_counter' >
{{lineNumber}}
</div>
組件:在你的代碼
import {Component, OnInit, Input, ElementRef, ViewChild} from '@angular/core';
import {Globals} from "../../../globals";
@Component({
selector: 'app-line-counter',
templateUrl: './line-counter.component.html',
styleUrls: ['./line-counter.component.css']
})
export class LineCounterComponent implements OnInit {
@ViewChild('#refId') textDiv:ElementRef;
@Input() lineNumber : number = 0;
constructor() {
}
ngOnInit() {
if(Globals.refLineCounter == null) {
Globals.refLineCounter = this;
//console.log(Globals.refLineCounter.getHeight());
console.log(this.getHeight());
}
}
getHeight(){
return this.textDiv.nativeElement.height;
}
}