英文:
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 = '<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>
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('<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>');
constructor(private sanitized: DomSanitizer) {}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论