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

87
Visualizações
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 Respostas
Responde à pergunta

0

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

about 4 years ago · Santiago Trujillo 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