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

348
Views
¿Cómo esperar que se ejecute ngAfterViewInit() mientras se crea una instancia de un componente a partir de una directiva?

Tengo una directiva que crea en el código una instancia de la plantilla de componente que usa y establece su HTML interno, que cambiará la dimensión de la plantilla:

 var factory = this.resolver.resolveComponentFactory(MyComponentTemplate); this.templateComponent = this.viewContainerRef.createComponent(factory); this.templateComponent.instance.htmlStr = anyText;

Ahora aquí está el problema. En esta etapa, no definiré los tamaños de los componentes:

 console.log(this.templateComponent.instance.width); //undefined console.log(this.templateComponent.instance.height); //undefined

En la depuración, noté que solo después de que mi componente ejecuta ngAfterViewInit() , solo entonces puedo leer el ancho y la altura de la plantilla del componente de mi directiva y usar esos valores.

¿Hay alguna manera de decirle a mi directiva que espere hasta que finalice ngAfterViewInit() y luego haga lo que necesito de mi directiva con la información que estoy buscando?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

constructor(private cdRef:ChangeDetectorRef) {} ... var factory = this.resolver.resolveComponentFactory(MyComponentTemplate); this.templateComponent = this.viewContainerRef.createComponent(factory); this.templateComponent.instance.htmlStr = anyText; this.cdRef.detectChanges(); // run change detection immediately console.log(this.templateComponent.instance.width); //undefined console.log(this.templateComponent.instance.height); //undefined
over 4 years ago · Santiago Trujillo Report

0

El uso de un ChangeDetectorRef inyectado globalmente no es necesario y es posible que no funcione. Puede confiar en el método changeDetectorRef de ComponentRef :

 var factory = this.resolver.resolveComponentFactory(MyComponentTemplate); this.templateComponent = this.viewContainerRef.createComponent(factory); this.templateComponent.instance.htmlStr = anyText; this.templateComponent.changeDetectorRef.detectChanges();
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!