我需要知道渲染元素的高度。我不在乎元素是否报告它或父项是否检测到高度。
我一直在研究这个问题,我的第一个根本原因是 :host 元素必须有它的display:block
集合,我已经做到了。(我做得对吗?)
this.nativeElement
将重要信息转储到 console.log 但是当我尝试以编程方式访问该值时,它出现“0”。
文件:
img-fader.component.html
<img [src]='imgSrc' class="item carousel-item" style="width: 100%; display: block;"/>
img-fader.component.css
:host {
display: block;
}
img.carousel-item {
position: absolute;
left: 0;
}
img-fader.component.ts(重要部分)
export class ImgFaderComponent implements AfterViewInit {
@ContentChildren(ImgFaderComponent, {read: ElementRef}) imgList: QueryList<ElementRef>;
constructor() {}
ngAfterViewInit() {
this.imgList.forEach(img => console.log('Img: ', img.nativeElement.children[0].offsetHeight));
}
}