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

203
Visualizações
navigateByURL with hashtag lands in wrong position - Angular routerLink inside router-outlet

enter image description hereA link to switch a page between router-outlet doesn't work well, it moves to a correct page but it lands in the middle of the page (somewhere under router-outlet) instead of header where I've set focus.

  • RouterLink works between outside of router-outlet(navigation) and router-outlet.
  • If I remove fragment/focusDirective, it lands on top on the page, but I need to have fragment in order for screen reader to read up the header.
  • Header becomes focused (tested with NerdeFocus chrome extension) when the page has been rendered, so focus function seems working
  • I tried to add scrollIntoView function in app component, but it didn't help
  • I'm using Angular11

app.ts

<nav class="col-lg-3">
....
</nav>

<div class="col-lg-9">
   <router-outlet></router-outlet>
</div>

app-routing.module.ts

const routes: Routes = [
  {
    path: 'a',
    component: AComponent,
  },
  {
    path: 'b',
    component: BComponent,
  },
];

@NgModule({
  imports: [
    RouterModule.forRoot(routes, {
    useHash: true,
    relativeLinkResolution: 'legacy'
}),
  exports: [RouterModule]
})
export class AppRoutingModule { }

Component A html

<a (click)="onEditMode($event)">Edit</a>

Component A ts

onEditMode(event) {
  event.preventDefault();
  this.router.navigateByUrl('componentB#header');
}

Component B html

<h1 id="header" tabindex="-1" appFocusFragment>Header</h1>

focusFragment

@Directive({
  selector: '[appFocusFragment]'
})

export class FocusFragmentDirective implements AfterViewInit{
  constructor(private route: ActivatedRoute) { }

  ngAfterViewInit() {
    this.route.fragment.subscribe(fragment => {
      const anchor = document.querySelector<HTMLAnchorElement>(`#${fragment}`);
      if (fragment && anchor) {
        anchor.scrollIntoView();
        anchor.focus();
      }
    });
  }
}

Sorry about my bad explanation and English, please kindly let me know if more information is needed.

Thank you!

about 4 years ago · Juan Pablo Isaza
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