Search code examples
angulartypescriptrouter-outlet

Angular 6 | RouterLink


is there any way to go to another page's section which is defined by id without reload


Solution

  • You can do using scrollIntoView method

    Credits goes to:ibenjelloun

    Example:https://stackblitz.com/edit/angular-scroll-spy-routing

    @HostListener('scroll', ['$event'])
    onScroll(event: any) {
        let currentSection: string;
        const children = this._el.nativeElement.children;
        const scrollTop = event.target.scrollTop;
        const parentOffset = event.target.offsetTop;
        for (let i = 0; i < children.length; i++) {
            const element = children[i];
            if (this.spiedTags.some(spiedTag => spiedTag === element.tagName)) {
                if ((element.offsetTop - parentOffset) <= scrollTop) {
                    currentSection = element.id;
                }
            }
        }
        if (currentSection !== this.currentSection) {
            this.currentSection = currentSection;
            this.sectionChange.emit(this.currentSection);
        }
    }