Javascript 角度-还有什么其他问题会导致视线偏离;0";?

Javascript 角度-还有什么其他问题会导致视线偏离;0";?,javascript,angular,offsetheight,Javascript,Angular,Offsetheight,我需要知道渲染元素的高度。我不在乎元素是否报告它,或者父元素是否检测到高度 我一直在研究这个问题,第一个根本原因是:host元素必须设置其display:block,我已经这样做了。(我做得对吗?) this.nativeElement将重要信息转储到console.log,但当我尝试以编程方式访问该值时,它显示为“0” 档案: img-fader.component.html <img [src]='imgSrc' class="item carousel-item" style="wi

我需要知道渲染元素的高度。我不在乎元素是否报告它,或者父元素是否检测到高度

我一直在研究这个问题,第一个根本原因是: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.ts(重要部分)

导出类ImgFaderComponent实现AfterViewInit{
@ContentChildren(ImgFaderComponent,{read:ElementRef})imgList:QueryList;
构造函数(){}
ngAfterViewInit(){
this.imgList.forEach(img=>console.log('img:',img.nativeElement.children[0].offsetHeight));
}
}

如果有人仍在为图像离视的类似问题而挣扎,我发现如果您使用setTimeout函数包装获取离视的命令,那么您就可以正确获得图像的高度(和宽度)


发生这种情况是因为异步命令运行到下一个周期,因此图像已加载。

img标记有加载事件。图像在加载OK之前没有维度,因此如何挂接到该事件?
:host {
    display: block;
}

img.carousel-item {
    position: absolute;
    left: 0;
}
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));
  }
}