Read value of an html script element in Angular, which is of type json


In the html page, there will be a script element which is a static content ( generated from a Django backend) with an ID.

<script id="hello-data" type="application/json">{"hello": "worldu003C/scriptu003Eu0026amp;"}</script>

I wanted to get that data in an angular service ( or even component) which would have a simple getElementById is javascript.

const value = JSON.parse(document.getElementById('hello-data').textContent);

I did a lot of searches, found answers pointing to "ViewChild", "@Inject(DOCUMENT)", but none of these works on a ‘script’ tag.

Any pointers to solution would be really appreciated 🙂

Source: Angular Questions

Categorized as angular, django, getelementbyid, javascript Tagged , , ,


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Still Have Questions?

Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us