I'm making an FAQ page that has all the questions at the top as hyperlinks. When you click on a hyperlink you get directed to the part on the page where the answer of the question can be read. For this I am trying to use the solution provided here Angular2 Routing with Hashtag to page anchor provided by Kaloyan. The solution breaks for me at the codeline
const element = document.querySelector("#" + tree.fragment);
This gives the error ERROR DOMException: Failed to execute 'querySelector' on 'Document': '#1' is not a valid selector.
. I can understand why i get this error because it is looking for a selector, and the input it gets is #fragment. Yet everyone in the stackoverflow thread i've mentioned says it works so maybe i am doing something wrong. When i replace querySelector with getElementById(tree.fragment) i no longer get errors but the page does not jump to the section when i click on one of the links either. Can someone tell me what part i'm doing wrong or what part i've missed implementing from the solutions at the thread mentioned? Thank you.
Heres my code:
Component code:
import { Component, OnInit } from '@angular/core';
import { FaqItem } from './faq-item.model';
import { Router, NavigationEnd } from '@angular/router';
@Component({
selector: 'app-faq',
templateUrl: './faq.component.html',
styleUrls: ['./faq.component.css']
})
export class FaqComponent implements OnInit {
faqItems: FaqItem[] = EXAMPLE_DATA;
constructor(router: Router) {
router.events.subscribe(s => {
if (s instanceof NavigationEnd) {
const tree = router.parseUrl(router.url);
if (tree.fragment) {
const element = document.querySelector("#" + tree.fragment);
// const element = document.getElementById(tree.fragment);
if (element) { element.scrollIntoView(true); }
}
}
});
}
ngOnInit() {
}
}
const EXAMPLE_DATA: FaqItem[] = [
{id: 1, question: 'How do i use the seedcalendar app?', answer: 'please check the getting started tutorial for this'},
{id: 1, question: 'How do i use the seedcalendar app?', answer: 'please check the getting started tutorial for this'},
{id: 1, question: 'How do i use the seedcalendar app?', answer: 'please check the getting started tutorial for this'},
{id: 1, question: 'How do i use the seedcalendar app?', answer: 'please check the getting started tutorial for this'},
{id: 1, question: 'How do i use the seedcalendar app?', answer: 'please check the getting started tutorial for this'},
{id: 1, question: 'How do i use the seedcalendar app?', answer: 'please check the getting started tutorial for this'},
{id: 1, question: 'How do i use the seedcalendar app?', answer: 'please check the getting started tutorial for this'},
{id: 1, question: 'How do i use the seedcalendar app?', answer: 'please check the getting started tutorial for this'},
{id: 1, question: 'How do i use the seedcalendar app?', answer: 'please check the getting started tutorial for this'},
{id: 1, question: 'How do i use the seedcalendar app?', answer: 'please check the getting started tutorial for this'},
{id: 1, question: 'How do i use the seedcalendar app?', answer: 'please check the getting started tutorial for this'},
{id: 1, question: 'How do i use the seedcalendar app?', answer: 'please check the getting started tutorial for this'},
{id: 1, question: 'How do i use the seedcalendar app?', answer: 'please check the getting started tutorial for this'},
{id: 1, question: 'How do i use the seedcalendar app?', answer: 'please check the getting started tutorial for this'},
{id: 1, question: 'How do i use the seedcalendar app?', answer: 'please check the getting started tutorial for this'},
{id: 1, question: 'How do i use the seedcalendar app?', answer: 'please check the getting started tutorial for this'},
{id: 1, question: 'How do i use the seedcalendar app?', answer: 'please check the getting started tutorial for this'},
{id: 1, question: 'How do i use the seedcalendar app?', answer: 'please check the getting started tutorial for this'},
{id: 1, question: 'How do i use the seedcalendar app?', answer: 'please check the getting started tutorial for this'}, {id: 1, question: 'How do i use the seedcalendar app?', answer: 'please check the getting started tutorial for this'},
{id: 1, question: 'How do i use the seedcalendar app?', answer: 'please check the getting started tutorial for this'},
{id: 1, question: 'How do i use the seedcalendar app?', answer: 'please check the getting started tutorial for this'},
{id: 1, question: 'How do i use the seedcalendar app?', answer: 'please check the getting started tutorial for this'},
{id: 1, question: 'How do i use the seedcalendar app?', answer: 'please check the getting started tutorial for this'},
{id: 1, question: 'How do i use the seedcalendar app?', answer: 'please check the getting started tutorial for this'},
{id: 1, question: 'How do i use the seedcalendar app?', answer: 'please check the getting started tutorial for this'},
{id: 1, question: 'How do i use the seedcalendar app?', answer: 'please check the getting started tutorial for this'},
{id: 1, question: 'How do i use the seedcalendar app?', answer: 'please check the getting started tutorial for this'},
{id: 1, question: 'How do i use the seedcalendar app?', answer: 'please check the getting started tutorial for this'},
{id: 1, question: 'How do i use the seedcalendar app?', answer: 'please check the getting started tutorial for this'},
{id: 1, question: 'How do i use the seedcalendar app?', answer: 'please check the getting started tutorial for this'},
{id: 1, question: 'How do i use the seedcalendar app?', answer: 'please check the getting started tutorial for this'},
{id: 1, question: 'How do i use the seedcalendar app?', answer: 'please check the getting started tutorial for this'},
{id: 1, question: 'How do i use the seedcalendar app?', answer: 'please check the getting started tutorial for this'},
{id: 1, question: 'How do i use the seedcalendar app?', answer: 'please check the getting started tutorial for this'},
{id: 1, question: 'How do i use the seedcalendar app?', answer: 'please check the getting started tutorial for this'},
{id: 1, question: 'How do i use the seedcalendar app?', answer: 'please check the getting started tutorial for this'},
{id: 1, question: 'How do i use the seedcalendar app?', answer: 'please check the getting started tutorial for this'},
{id: 1, question: 'How do i use the seedcalendar app?', answer: 'please check the getting started tutorial for this'},
{id: 1, question: 'How do i use the seedcalendar app?', answer: 'please check the getting started tutorial for this'},
{id: 1, question: 'How do i use the seedcalendar app?', answer: 'please check the getting started tutorial for this'},
{id: 1, question: 'How do i use the seedcalendar app?', answer: 'please check the getting started tutorial for this'},
{id: 1, question: 'How do i use the seedcalendar app?', answer: 'please check the getting started tutorial for this'}, {id: 1, question: 'How do i use the seedcalendar app?', answer: 'please check the getting started tutorial for this'},
{id: 1, question: 'How do i use the seedcalendar app?', answer: 'please check the getting started tutorial for this'},
{id: 1, question: 'How do i use the seedcalendar app?', answer: 'please check the getting started tutorial for this'},
{id: 1, question: 'How do i use the seedcalendar app?', answer: 'please check the getting started tutorial for this'},
{id: 1, question: 'How do i use the seedcalendar app?', answer: 'please check the getting started tutorial for this'},
{id: 1, question: 'How do i use the seedcalendar app?', answer: 'please check the getting started tutorial for this'},
]
export class FaqItem {
id: number;
question: string;
answer: string;
}
The HTML code:
<section id="top">
<ol>
<li *ngFor="let item of faqItems; let i=index">
<a [routerLink]="['.']" fragment={{i}}>{{item.question}}</a>
</li>
</ol>
<dl>
<app-faq-item *ngFor="let item of faqItems; let i=index"
[question]="item.question" [answer]="item.answer" [attr.id]="i">
</app-faq-item>
</dl>
</section>