My goal is to extract the SVG code generated by my own component from the DOM. Currently, I am attempting to achieve this using the following method:
<my-own-component id="my-component-id" #myComponentId
[data]="data"></my-own-component>
onButtonClick() {
this.data = someData;
const svgCode = document.getElementById('my-component-id').innerHTML;
}
I have also attempted an alternative approach (which also did not work):
@ViewChild('myComponentId') myComponentId;
...
onButtonClick() {
this.data = someData;
const svgCode = this.myComponentId.nativeElement.children[0].innerHTML;
}
The challenge arises because I am retrieving the content prior to Angular applying the changes triggered by this.data = someData
, resulting in the SVG elements not being captured.
To workaround this issue, I resorted to using a 50ms timeout. Although effective, it is not an ideal solution and merely serves as a temporary fix:
this.data = someData;
await new Promise(resolve => setTimeout(resolve.bind(null, null), 50));
const svgCode = document.getElementById('my-component-id').innerHTML;
I am seeking a more appropriate approach that allows me to wait for Angular to complete rendering the component. Is there a recommended method to accomplish this?
Thank you in advance.