Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

223
Vistas
Angular 2: Getting Popup Height from DOM

I have a "popup" that displays data retrieved from a database on mousemove and appears at the mouse location. It looks like this:

<div (mousemove)="onMouseMove($event)></div>

onMouseMove(event: MouseEvent) {
    this.xCoord = event.x
    this.yCoord = event.y
}

<div [style.left.px]="xCoord" [style.top.px]="yCoord" class="popup">
    ...
</div>

Because the dimensions of the popup are dynamic, depending on its content, I need to retrieve the dimensions in order for the popup to be placed properly (above the target and centered horizontally). Is it possible in Angular 2 to get style properties from an element without, for example, a mouse event? Ideally, the dimensions are assigned to a variable when the popup appears.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Why don't you just use basic Javascript to access the DOM element's width and height properties, like so:

Javascript:

getPopupDimentions() {
   let myElement = getElementById("myPopup");
   return {
      width: myElement.style.width,
      height: myElement.style.height
   }
}

HTML:

<div [style.left.px]="xCoord" [style.top.px]="yCoord" class="popup" id="myPopup">
    ...
</div>

Of course, the Javascript I posted above will need to be called once the popup has been initiated.


If you wanted a more Angular like approach, I would recommend using a View Child, like so:

Javascript:

Make sure you import ViewChild import { Component, ViewChild } from '@angular/core'

@ViewChild('mypopup') mypopup; 

getPopupDimentions() {
   return {
      width: this.mypopup.nativeElement.style.width,
      height: this.mypopup.nativeElement.style.height
   }
}

HTML:

<div [style.left.px]="xCoord" [style.top.px]="yCoord" class="popup" #mypopup>
    ...
</div>

If you want to find out more about Angular view children, I recommend checking out this question and its answers.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda