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

265
Visualizações
String from api response cant display break line, but hard code string can display break line

Good day,

I am using Angular 8 as my frontend, and java api service as my backend.

I need to retrieve some String from backend, and the String will having \n in between, for example: "Instructions:\n1. Key in 122<16 digit \nreload pin># to reload.\n2. Press SEND/CALL from

In my .ts file, I am setting this String value as follow:

this.str = this.apiRes.responseMsg1;
console.log("this.str : " + this.str);

This will give me Instructions:\n1. Key in 122<16 digit \nreload pin># to reload.\n2. Press SEND/CALL, thus when I use it to display in html, it will just display as 1 line.

If I hard code this String to a String variable, for example:

this.str = "Instructions:\n1. Key in 122<16 digit \nreload pin># to reload.\n2. Press SEND/CALL from";
console.log("this.str : " + this.str);

It will give me :

Instructions:
1. Key in 122<16 digit 
reload pin># to reload.
2. Press SEND/CALL from 

Which is what I want.

I am not really familiar with Angular, I am trying to find this answer in google, but cant get any related result.

May I know why is this happen? And any way I can display the api message accordingly?

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

0

HTML ignores \n line breaks. You basically have two options to fix this:

  1. Replace the line breaks with <br> elements.
  2. Add a line of css white-space: pre-wrap; to your HTML element which is displaying your string.
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