javascript - Angular 在字符串中查找标记并用真实的 html 标记替换
问题描述
我从后端得到一个字符串,当我显示它时,它< br/>
看起来像文本。
我想将它们转换为真正的 HTML 标记或用真正的断线替换它。
这是从后端检索到的字符串:
Description from backend Description from <br/> backend Description
from backend Description from backend <br/>Description <br/>from <br/>
backend Description from backend <br/> Description from backend
Description from backend Description from backend
这是我的功能:
ngOnInit(): void {
this.uns = this.sentence$.subscribe(res => {
this.sentence = res;
if (this.sentence && this.sentence.length > 0) {
this.sentence = res.replace('<br/>', '\n');
}
});
}
解决方案
HTML不会仅仅因为你有它们而打印你的空白字符,它只会打印第一个空格然后跳过其余的。
您可以使用<pre></pre>
标签来解决这个问题,然后您可以使用类似的东西sentence = sentence.split("<br>").join("\n")
来解决这个<br/>
问题。
推荐阅读
- postgresql - 如何在 postgresql 中的 JSONB 列上添加 SQLAlchemy BTREE 索引
- python - 使用 Python OpenCV 测量已知圆的精确位置
- docker - 通过 TLS 连接到 OPENLDAP:找不到证书
- java - 在方法内更改应该在方法外的字符串
- linux - pci 驱动程序函数是否表示为 PCI 的 driver_data 对象中的函数号
- automation - 使用 QuerySerge 在 ALM 测试用例步骤级别附加屏幕截图
- reactjs - 我想在 React Native 中将 My Screen 修改为 Drawer
- security - 这些包含指向我的节点服务器的随机 URL 的 POST 请求是什么
- c# - 游戏有时无法识别触摸
- jquery - 展开并滚动到隐藏元素