【发布时间】:2018-10-01 09:08:48
【问题描述】:
我在 Angular 5 中实现了一个通用工具提示。为了正确定位(尤其是相对于目标元素居中),我需要在渲染它之前获取工具提示的宽度和高度。
我已经知道如何从this question 获取组件的尺寸。但是,我需要先设置工具提示的内容(让浏览器确定大小)来计算位置。然后,第二,我想设置位置并添加一个类来显示它。
这是我的代码:
// tooltip.component.html
<div class="popup fade"
[ngClass]="tooltip?.isOpen ? 'show' : ''"
[style.left.px]="calculatedPos?.left"
[style.top.px]="calculatedPos?.top">
{{ tooltip?.text }}
</div>
// tooltip.component.ts
export class TooltipComponent {
public tooltip: any;
public calculatedPos: TooltipPosition = {
left: 0,
top: 0
}
constructor(private store: Store<any>, private ref: ElementRef) {
this.store.subscribe((state) => this.render(state));
}
render () {
let targetRect = this.tooltip.targetEl
&& this.tooltip.targetEl.nativeElement
&& this.tooltip.targetEl.nativeElement.getBoundingClientRect()
|| null;
let tooltipRect = this.ref
&& this.ref.nativeElement
&& this.ref.nativeElement.getBoundingClientRect()
|| null;
if (targetRect && tooltipRect) { // <-- tooltipRect.height /.width is zero here!
switch (this.tooltip.placement) {
case 'right':
this.calculatedPos = this.calculatePositionRight(targetRect, tooltipRect, this.tooltip.offset)
break;
// and so on...
}
}
我尝试使用生命周期钩子:
-
ngAfterContentChecked导致相同的结果 -
ngAfterViewChecked给了我一个(可预见的)ExpressionChangedAfterItHasBeenCheckedError -
渲染时根本不调用
ngOnChanges
所以:如何在渲染内容之前获取组件的尺寸?或者如何检测正在呈现的内容然后更新位置?
我的下一个想法是使用超时来首先让组件呈现,然后设置位置(没有用户注意到) - 但也许有一个最佳实践,我还不知道。
【问题讨论】:
-
您无法从尚未由 DOM 计算的元素中获取高度。所以就像你建议的那样;让它渲染和隐藏,或者给元素一个固定的宽度。注意使用
%设置宽度也是可以的。
标签: angular typescript