Quiero navegar de una página a otra y saltar directamente a un control en el medio de la página de destino. Normalmente, podemos usar un hashtag "#" seguido del nombre o ID del control.
Sin embargo, este método funciona muy bien en Angular 2. He probado métodos comoAngular2 Routing with Hashtag to page anchor . Con ese método, la URL termina con "#myId", pero la página en realidad no salta.
¿Tenemos otras formas de lograr esto?
El proyecto de documentación angular tiene el servicio AutoScrollService. Puedes intentar usarlo. desplazamiento automático.servicio.ts
/** * A service that supports automatically scrolling elements into view */ @Injectable() export class AutoScrollService { constructor( @Inject(DOCUMENT) private document: any, private location: PlatformLocation) { } /** * Scroll to the element with id extracted from the current location hash fragment * Scroll to top if no hash * Don't scroll if hash not found */ scroll() { const hash = this.getCurrentHash(); const element: HTMLElement = hash ? this.document.getElementById(hash) : this.document.getElementById('top-of-page') || this.document.body; if (element) { element.scrollIntoView(); if (window && window.scrollBy) { window.scrollBy(0, -80); } } } /** * We can get the hash fragment from the `PlatformLocation` but * it needs the `#` char removing from the front. */ private getCurrentHash() { return this.location.hash.replace(/^#/, ''); } }