Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

264
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda