Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

244
Views
¿Cómo obtener el tamaño de un componente secundario en angular 2?

Estoy tratando de centrar un modal. Contiene otros componentes, por lo que la posición depende del contenido. Estoy tratando de obtenerlo de esta manera, que no funciona y no estoy seguro de cómo solucionarlo:

componente HTML:

 <div #modal [style.top]="positionTop" [style.left]="positionLeft"> <app-modal-one *ngIf="feature==='one'"> </app-modal-one> <app-modal-two *ngIf="feature==='two'"></app-modal-two> <app-modal-three *ngIf="feature==='three'"></app-modal-three> </div>

componente ts:

 @ViewChild('modal') private modalContent: ElementRef; ngOnInit() { this.modalHeight = this.modalContent.nativeElement.offsetHeight; this.modalWidth = this.modalContent.nativeElement.offsetWidth; this.positionLeft = (window.innerWidth - this.modalWidth) / 2 + "px"; this.positionTop = (window.innerHeight - this.modalHeight) / 2 + "px"; console.log("on init: "+this.modalWidth) }

El registro de la consola muestra el tamaño como 0. Si lo reviso más tarde una vez que se muestra, muestra el tamaño correcto. ¿Cuándo y cómo puedo obtener este tamaño (del #modal) antes o justo después de que se muestre todo?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

constructor(private cdRef:ChangeDetectorRef){} @ViewChild('modal') private modalContent: ElementRef; ngAfterViewInit() { this.modalHeight = this.modalContent.nativeElement.offsetHeight; this.modalWidth = this.modalContent.nativeElement.offsetWidth; this.positionLeft = (window.innerWidth - this.modalWidth) / 2 + "px"; this.positionTop = (window.innerHeight - this.modalHeight) / 2 + "px"; console.log("on init: "+this.modalWidth) }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!