Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

216
Views
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": "world\\u003C/script\\u003E\\u0026amp;"}</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 :)

Edits: Below is what I have tried with DOCUMENT

  constructor(@Inject(DOCUMENT) document: Document) {
    let el = document.getElementById('hello-data'); 
    console.log(el); // This prints null
    //this.data = JSON.parse(el.textContent);

  }

Interestingly, if I tired to access any other element by ID, it works. I see the issue only with script tag.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!