简体   繁体   中英

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. ie < would show up in the HTML markup as &lt; . But I want to wrap the user entered value with actual html. The idea is that I should be able to escape the user entered value, yet still trust the html.

Here is my html snippet: <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. ie if they entered Seattle <rocks> it would output the html as 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.

Well, I found this: Convert special characters to HTML in Javascript

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.

I hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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