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

271
Visualizações
Place innerHTML in element as a text not html

I'm building messenger app using AngularJS . messagesData is an array that contains message type and message content and sent by who .

When I use this function it works perfect ,

But when I type any html code in the function the html get applied in the message box

Example :

input("<div>Hello</div>")

it creates a div inside of it the hello word but I want the whole element to appear in the message

This is the function that i'm talking about :

function dataToHtml(which){
            //check if data = message
            if(messagesData[which][0]=='message'){
                //check if its user message or the other one
                if(messagesData[which][1]=='mine'){
                    chatContainer.innerHTML+=
                    `
                    <div class="chat-message-box">
                        <div class="mine-message chat-message">${messagesData[which][2]}</div>
                    </div>
                    `
                }else if(messagesData[which][1]=='other'){
                    chatContainer.innerHTML+=
                    `
                    <div class="chat-message-box">
                        <div class="other-message chat-message main-bg">${messagesData[which][2]}</div>
                    </div>
                    `
                }
            }
        }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Use one way flow syntax property binding:

comment: string;
comment = "<p><em><strong>abc</strong></em></p>";
<div [innerHTML]="comment"></div>

From angular docs: "Angular recognizes the value as unsafe and automatically sanitizes it, which removes the tag but keeps safe content such as the element." https://angular.io/guide/security#sanitization-example

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