Javascript 仅AngularJs渲染
我有一个设想。我为用户提供了一个评论框,用户可以在其中添加新行评论,如:Javascript 仅AngularJs渲染,javascript,angularjs,html,xss,Javascript,Angularjs,Html,Xss,我有一个设想。我为用户提供了一个评论框,用户可以在其中添加新行评论,如: This is my comment. Part of comment in new line 我正在数据库中保存为: model.Description = model.Comment.Replace("\n", "<br />"); //Insert into DB 现在,当在AngularJs视图上显示它时,它显示了如下内容: This is my comment. <br /> Part
This is my comment.
Part of comment in new line
我正在数据库中保存为:
model.Description = model.Comment.Replace("\n", "<br />");
//Insert into DB
现在,当在AngularJs视图上显示它时,它显示了如下内容:
This is my comment. <br /> Part of comment in new line
**我的html:**
<style>
.angular-with-newlines {
white-space: pre;
}
</style>
<div class="ads-details-info col-md-8 angular-with-newlines">
{{comment}}
</div>
输出:
我想渲染为新线。我知道有一些可用的解决方案,如:trustAsHtml和ng bind html。但是,它很容易受到XSS攻击。这是我不想要的。我只想允许一些选定的标记。有什么解决办法吗
我看到了。但解决方案对我来说并不奏效。我不希望注释被呈现为代码和空白:pre;不适合我 像这样与ng绑定html一起使用,然后将被呈现为html表达式
<div ng-bind-html="expression" ></div>
我也看到了这个解决方案。在我的情况下,这不是解决办法。仅供参考,没有起作用。它将开始显示为代码。我不想将注释显示为代码。副本中接受的答案不是很好。阅读其他答案。我已经尝试了那篇文章中给出的所有解决方案。什么都没用。均匀空白:预包装;没用,我很怀疑。是否意识到如果使用预换行,则不应替换换行符?是的,我将替换为\n,但不起作用\是的,我知道,但是它很容易受到XSS攻击。所以我不打算使用它。@UsmanKhalid您可以使用“消毒”使其安全:
<div ng-bind-html="expression" ></div>