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

160
Visualizações
Adding ngIf to script tag in html not working

I am trying to add a src to a script in my html file, but only if a condition is true. Otherwise it should not have run the script src. Below is my code so far:

    <script>
      this.booleanValue = JSON.parse(
        sessionStorage.getItem("booleanValue")
      );

      if (this.booleanValue == "true") {
        console.log("true");
      }
    </script>
    <script
      *ngIf="this.booleanValue != true"
      src="code.js"
      async
    ></script>

It console logs the booleanValue as true correctly, however it still runs the script src. It shouldn't because it should only run when false. Does anyone know what I'm doing wrong?

Thanks in advance

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You're JSON parsing "booleanValue" when doing :

JSON.parse(
    sessionStorage.getItem("booleanValue")
  )

And you're trying to get value of this.booleanValue directly, it'd be something like 'this.booleanValue.value' when verifying if its value is true or not, or you don't use JSON.parse().

Example: If Session variable is booleanValue => true, you use if(this.booleanValue), but if Session variable is booleanValue => "{value:true}" or "{\"value\":true}", then you use JSON.parse and if(this.booleanValue.value)

about 4 years ago · Juan Pablo Isaza Relatório
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