【问题标题】:How to use/display string with html tags in typescript如何在打字稿中使用/显示带有html标签的字符串
【发布时间】:2019-08-17 14:01:09
【问题描述】:
var bannerText = stringFormat("This is my {0}test content{1} here", "<b>", "</b>");
<div>
    <p> {para1}</p>
    <p> {bannerText}</p>
</div>

html标签被打印而不是作为文本应用

观察到的输出

这是我的测试内容这里

需要的输出

这是我的测试内容这里

【问题讨论】:

标签: html typescript


【解决方案1】:
<div [innerHTML]="bannerText"></div>

但是,请记住 Angular 的 docs about security,上面写着 Angular recognizes the value as unsafe and automatically sanitizes it, which removes the &lt;script&gt; tag but keeps safe content such as the &lt;b&gt; element.

【讨论】:

    【解决方案2】:

    检查下面的代码可能对你有帮助。使用内部 html

    document.getElementById("name").innerHTML="This is &lt;u&gt;my test&lt;/u&gt; sample"
    &lt;div id="name"&gt;&lt;/div&gt;

    .

    【讨论】:

      【解决方案3】:

      Observed output This is my &lt;u&gt;test content&lt;/u&gt; here

      您使用的任何方法都是 转义 html,例如&lt; / &gt;

      修复

      您需要一些版本的设置 HTML

      例如反应:dangerouslySetInnerHTML,纯 JS:使用innerHTML

      警告

      小心,设置 html 会将您的应用暴露给 XSS:https://en.wikipedia.org/wiki/Cross-site_scripting

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-11-29
        • 2021-11-05
        • 1970-01-01
        • 2021-10-18
        • 2021-11-04
        • 2012-10-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多