I am using this answer to convert a HTML string to DOM elements in Angular.
The problem is that I can't get attributes of Node
. getAttribute()
cannot be used since typescript will complain that No property getAttribute on Node
.
Code is below (simplified).
import { AfterViewInit, Renderer2 } from '@angular/core';
export class MockComponent implements AfterViewInit {
constructor(private renderer: Renderer2) {}
private mockString = '<a href="#test1">test 1</a>'
ngAfterViewInit(): void {
const template = this.renderer.createElement('template');
template.innerHTML = this.mockString.trim();
const anchorNodes: NodeList = template.content.querySelectorAll('a');
const anchors: Node[] = Array.from(anchorNodes);
for (const anchor of anchors) {
// how to get attributes of anchor here?
// anchor.getAttribute('href') will be complained
}
}
}