Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

215
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda