简体   繁体   English

如何在字符串中转义html实体

[英]How to escape html entities within a string

I wish to escape the contents of the user entered value, so they show up as html entities. 我希望转义用户输入的值的内容,因此它们显示为html实体。 ie < would show up in the HTML markup as &lt; 即<将在HTML标记中显示为&lt; . But I want to wrap the user entered value with actual html. 但是我想用实际的html包装用户输入的值。 The idea is that I should be able to escape the user entered value, yet still trust the html. 我的想法是我应该能够逃避用户输入的值,但仍然信任html。

Here is my html snippet: <span ng-bind-html="trustHtml(notif.getConditionText())"></span> 这是我的html代码段: <span ng-bind-html="trustHtml(notif.getConditionText())"></span>

Controller: 控制器:

$scope.trustHtml = function(html) {
    return $sce.trustAsHtml(html);
}

Notif: 通知:

getConditionText: function() {
    return "<b>" + $sanitize(this.name) + "</b>";
}

I'm looking for a function that would go in place of $sanitize that would escape the user entered "name" property value. 我正在寻找一个可以代替$ sanitize的函数,该函数可以使用户输入的“ name”属性值避开。 ie if they entered Seattle <rocks> it would output the html as Seattle &lt;rocks&gt; 也就是说,如果他们输入Seattle <rocks> ,则会将HTML输出为Seattle &lt;rocks&gt;

Anyone know of something like this for angular? 有人知道像这样的东西吗?

Note I am not trying to encode to URI entities, but HTML entities. 注意我不是要编码为URI实体,而是HTML实体。

Well, I found this: Convert special characters to HTML in Javascript 好吧,我发现了这一点: 将特殊字符转换为Javascript中的HTML

where u can write a function like: 您可以在其中编写如下功能:

function HtmlEncode(s)
{
  var el = document.createElement("div");
  el.innerText = el.textContent = s;
  s = el.innerHTML;
  return s;
}

and get it encoded. 并进行编码。 Now, I didn't found something specific to angularjs. 现在,我没有找到特定于angularjs的东西。

I hope it helps 希望对您有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM