API响应的字符串无法显示断路线,但是硬码字符串可以显示断路线

发布于 2025-02-03 06:47:13 字数 742 浏览 4 评论 0原文

美好的一天,

我将Angular 8用作前端,而Java API服务作为我的后端。

我需要从后端检索一些字符串,并且该字符串将在两者之间具有\ n,例如: pin>##reload。

“指令:\ n1。键在122< 16 digit \ nreload 设置此字符串值如下:

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

这将为我提供指令:\ n1。 为1行

在HTML中显示,如果我将此字符串硬编码

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

,例如:它将给我:

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

的。

这是我想要 在Google中找到这个答案,但是

我可以知道为什么会发生这种情况?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

开始看清了 2025-02-10 06:47:13

html忽略\ n线路断裂。基本上,您有两个选择可以解决此问题:

  1. &lt; br&gt; elements替换销售线断裂。
  2. 将CSS 白空间的线添加到显示您的字符串的HTML元素中。

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文