Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

356
Visualizações
How can I get the width and height of a component in angular?

I've designed a loading component for the forms input elements in my angular forms to be displayed while the fields are loaded from the api. Here, I have to provide the height and the width of the component to the app-loader component so that it can occupy space accordingly.

For now what I have done is manually use a switch-case and provide heights and widths within that. However, this is hard since I have many other input types to be developed and also it's even harder to make it responsive. My question is, is there a more efficient way to dynamically pass the height and the width of the component relevant to that particular switch case to the app-loader component?

What I have so far is shown below.

<ng-container
  *ngIf="!formInput.loaded"
  [ngSwitch]="formInput.type">
  <div *ngSwitchCase="FORM_INPUT_TYPE.CHECKBOX">
    <app-loader width="409" height="24"></app-loader>
  </div>
  <div *ngSwitchCase="FORM_INPUT_TYPE.IMAGE_DROP_AREA">
    <app-loader width="414.66" height="96"></app-loader>
  </div>
  <div *ngSwitchCase="FORM_INPUT_TYPE.RATING">
    <app-loader width="409" height="30"></app-loader>
  </div>
  <div *ngSwitchDefault>
    <app-loader width="645" height="39.75"></app-loader>
  </div>
</ng-container>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Inject the element ref of the component in the constructor like this:

...
private elem: ElementRef,
...

Then you can get the native element by this.elem.nativeElement. Then you can get the height and width by offsetHeight and offsetWidth. If you want a particular div, just use @ViewChild() and do the same.

Tip: Usually you will retrieve the height and width in the ngOnInit. However, depending on your situation, you may want to do it in ngAfterViewInit, or subscribe to the scroll event with the @HostBinding() directive.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda