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

85
Vistas
the component not loaded in ngafterViewInit

I need to implement scrool with an anchor (in my case skip the link).

<div class="skip-link" >
    <a accesskey="1" tabindex="0" id="skiplink1" [routerLink]="url" [fragment]="SKIP_LINK_URL"> {{ 'APPLICATION.HEADER.a' | translate }}</a>
</div>


<app-header>

</app-header>
<router-outlet [id]="SKIP_LINK_URL" style="position: relative"></router-outlet>

TO PASS THE URL IN MY TS I DO:

ngAfterViewInit(): void {   

    this.router.events.subscribe((ev) => {
      if (ev instanceof NavigationEnd) {

          let locationUrl: string = ev.url;   
    
       
           const firstHash= locationUrl.indexOf('#'+this.SKIP_LINK_URL);
           if (firstHash!== -1) {
               locationUrl = locationUrl.substring(0, firstHash);
             
                this.scrollBottom();          //here is the problem because I think the component is not created
             
           }
        this.url = locationUrl;     
     
    
      }
    }


scrollBottom() { 
    this.viewportScroller.scrollToAnchor(this.SKIP_LINK_URL);
   
  }

The problem is this.scrollBottom(); because when this method is executed the component is not created. I know that the method ngAfterViewInit is called after the view is created so I don't why there is this problem. I try to put a set interval in this.scrollBottom(); and the programs works fine, but I don't want use an interval solution. Can anyone help me?

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

0

Can you do it using ngAfterViewChecked() instead of ngAfterViewInit()

about 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