I don't know WHY this happens, but it can be fixed when setting the styles via renderer2.
So instead of [ngStyle] and this.contextMenuStyles = ... i simply use:
this.renderer.setStyle(this.contextMenu.nativeElement, 'top', top);
this.renderer.setStyle(this.contextMenu.nativeElement, 'left', left);
This can be done in a more elegant way using a helper function for multiple styles. Like this for example:
Class XXX {
constructor(private renderer: Renderer2) {}
onContextmenu($event: MouseEvent) {
$event.preventDefault();
const setStyles = <DomElement, StyleObj>(domEl: DomElement, styles: StyleObj) => {
Object.keys(styles).forEach((styleName) => {
this.renderer.setStyle(domEl, styleName, styles[styleName]);
});
};
setStyles(this.contextMenu.nativeElement, {
top: `${$event.y}px`,
left: `${$event.x}px`,
});
console.log(this.contextMenu.nativeElement.getBoundingClientRect());
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…