【发布时间】:2021-07-15 08:20:44
【问题描述】:
我正在尝试使用getBoundingClientRect().height 或this.elementView.nativeElement.offsetHeight 获取此<div class="bundles-wrap enterprise" 元素的像素高度。
这是我的 HTML:
<div class="row d-flex align-items-end" *ngFor="let value of valueList">
<!-- start-->
<div class="bundles-wrap enterprise" id="card-id" #card>
<div class="hdr">
<h2>{{value.Name}}</h2>
<span>What you’ll get</span>
</div>
</div>
</div>
这是我的组件类:
valueList: Model[];
@ViewChild('card', {read: ElementRef, static:false}) elementView: ElementRef;
ngAfterViewInit(): void{
this.loaderService.showLoader("shell-content-loader", "Processing…");
this.getValueList();
}
getValueList(){
this.service.getvalueDetails().subscribe(res => {
this.valueList = res;
if (this.valueList.length != 0) {
this.loaderService.hideLoader("shell-content-loader");
const height1 = document.getElementById('card-id').getBoundingClientRect().height;
const height2 = this.elementView.nativeElement.offsetHeight;
console.log(height1);
}
});
}
我正在尝试获取height1, height2,但它显示Cannot read property 'getBoundingClientRect' of null in Angular。没有任何作用。
【问题讨论】:
-
您尝试通过其 id 获取元素,并且该元素位于循环内,因此它不会是唯一的。不是问题吗?
-
有解决办法吗?
标签: html angular getelementbyid getboundingclientrect offsetheight