将顶部和左侧设置为ViewChild元素?Angular 4

Nem*_*a G 1 angular

我需要将offesetTop和offsetLeft设置为我的viewchild元素,以将其定位在另一个元素的悬停上.这是一个工具提示,所以这就是我尝试过的:

<div (mouseover)="showPopup($event, price)"></div>
<div #tooltip>Some text</div>
Run Code Online (Sandbox Code Playgroud)

这是我的div,在TS里面我有:

@ViewChild('tooltip') tooltip: ElementRef;

showPopup(event, price) {

    price.show = true;
    this.tooltip.nativeElement.offsetTop = event.target.offsetTop;
    this.tooltip.nativeElement.offsetLeft = event.target.offsetLeft;
  }
Run Code Online (Sandbox Code Playgroud)

但是我收到以下错误:

无法分配给对象'[object HTMLDivElement]'的只读属性'offsetTop'.有什么建议?

Gün*_*uer 8

offsetTop并且offsetLeft只在浏览器中读取,与Angular无任何关联.

也许这对你有用:

<div [style.top.px]="top" [style.left.px]="left">Some text</div>
Run Code Online (Sandbox Code Playgroud)
class MyComponent {
  top: number;
  left: number;
}
Run Code Online (Sandbox Code Playgroud)