Angular innerHTML不起作用,这个字符串文本。

huangapple go评论73阅读模式
英文:

Angular innerHTML not working this string text

问题

我遇到了一个问题,我得到了一个字符串

const str = '<p>Please ensure Process Model diagram represents Functions adequately (boxes that represent an activity or group of activities that produce an outcome):</p><p><br></p><p><a href="https://www.google.com" rel="noopener noreferrer" target="_blank">Click here</a></p>';
<div [innerHTML]="str"></div>

我想删除这个段落和其他的HTML结构,尝试使用innerHTML,但它没有去除HTML元素,不知道为什么。我们如何删除HTML元素并显示文本和链接?

英文:

I'm facing an issue I'm getting an string

const str = &#39;&lt;p&gt;Please ensure Process Model diagram represents Functions adequately (boxes that represent an activity or group of activities that produce an outcome):&lt;/p&gt;&lt;p&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&quot;https://www.google.com&quot; rel=&quot;noopener noreferrer&quot; target=&quot;_blank&quot;&gt;Click here&lt;/a&gt;&lt;/p&gt;&#39;
&lt;div [innerHTML]=&quot;str&quot;&gt;&lt;/div&gt;

I want to remove this paragraph and other HTML constructs, tried using innerHTML but its not stripping HTML elements, not sure why. How can we remove html elements and display text and link?

答案1

得分: 2

你需要使用DomSanitizer

public str = this.sanitized.bypassSecurityTrustHtml('<p>请确保流程模型图充分代表功能(代表活动或一组活动的方框,产生一个结果):</p><p><br></p><p><a href="https://www.google.com" rel="noopener noreferrer" target="_blank">点击这里</a></p>');

constructor(private sanitized: DomSanitizer) {}
英文:

You have to use DomSanitizer.

public str = this.sanitized.bypassSecurityTrustHtml(&#39;&lt;p&gt;Please ensure Process Model diagram represents Functions adequately (boxes that represent an activity or group of activities that produce an outcome):&lt;/p&gt;&lt;p&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&quot;https://www.google.com&quot; rel=&quot;noopener noreferrer&quot; target=&quot;_blank&quot;&gt;Click here&lt;/a&gt;&lt;/p&gt;&#39;);

constructor(private sanitized: DomSanitizer) {}

huangapple
  • 本文由 发表于 2023年7月27日 15:53:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76777584.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定