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

140
Vistas
Setting a CSS Id property inside ngOnInit not working in the browser

I'm having a little issue with setting a property height.

In my ngOnInit method I have this code below. When I step through it I can see the value of the property change in the console, but there's no change on screen or in the debugger. I also tried running this inside AfterViewInit but that didn't seem to do anything as well.

FYI - If I create a button and trigger it manually on the page I can make the change, it just doesn't work inside ngOnInit.

Any any one tell me why or if I'm doing something incorrectly?

ngOnInit(): void {
  if (something) {
    document.getElementById('map-full').style.setProperty('height', 'calc(100vh - 57px)');
  } else {
    document.getElementById('map-full').style.setProperty('height', 'calc(100vh - 105px)');
  }
}
<div id="map-full">

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

It does work as is: https://stackblitz.com/edit/angular-ivy-hupjkb?file=src%2Fapp%2Fhello.component.ts

You can change the if on line 10 and observe the 'Start Editing..' text shift up and down when the 'map' div changes height.

But this is not the idiomatic way to achieve this in angular:

  • you should avoid interacting with document and retrieving or manipulating the DOM in this manner.
  • it is preferred to keep styling in CSS rather than inline

A more typical approach is to use CSS and class bindings:

Template:

<div id="map-full" [class.full-height]="fullHeight || null">Map</div>

CSS

div { 
  height: calc(100vh - 105px)
}
div.full-height { 
  height: calc(100vh - 57px)
}

Component class:

fullHeight = false;

Changing the value of fullHeight will apply or remove the class from the div, which will apply the relevant CSS.

Stackblitz: https://stackblitz.com/edit/angular-ivy-gw4x8d?file=src%2Fapp%2Fhello2.component.ts

about 4 years ago · Juan Pablo Isaza 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