簡體   English   中英

將頂部和左側設置為ViewChild元素? 角4

[英]Set top and left to ViewChild element? Angular 4

我需要將offesetTop和offsetLeft設置為我的viewchild元素,以將其放置在另一個元素的懸停上。 這是一個工具提示,所以這是我嘗試的方法:

<div (mouseover)="showPopup($event, price)"></div>
<div #tooltip>Some text</div>

這是我的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;
  }

但是我收到以下錯誤:

無法將對象'[object HTMLDivElement]'的屬性'offsetTop'分配為只讀。 有什么建議嗎?

offsetTopoffsetLeft在瀏覽器中是只讀的,與Angular沒有任何關系。

也許這對您有用:

<div [style.top.px]="top" [style.left.px]="left">Some text</div>
class MyComponent {
  top: number;
  left: number;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM