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

88
Views
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 answers
Answer question

0

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

about 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!