使用 Angular 过滤器解码 HTML 实体

Decode HTML entities with Angular filter

本文关键字:HTML 实体 解码 过滤器 Angular 使用      更新时间:2023-09-26

我正在尝试使用和Angular JS过滤器从字符串中解码html实体。

我有一个如下所示的视图:

<div class="roboto medium-gray">
   <span class="item-description">{{item.description | plaintext}}</span>
</div>

截至目前,我正在应用一个去除标签的过滤器:

.filter('plaintext', function() {
  return function(text) {
    return text ? String(text).replace(/<[^>]+>/gm, '') : '';
  };
})

我正在尝试确定一种可以解码其中任何 html 环境的方法。

item.description "A large house with wrap around porch & pool"

现在,item.description通过plaintext过滤器后,它显示为:

A large house with wrap around porch &amp; pool

我希望它用&替换&amp;

提前感谢您的帮助。

您可以使用 ng-bind-html ,如下所示:

<div class="roboto medium-gray">
   <span class="item-description" ng-bind-html="item.description | plaintext"></span>
</div>

有关更多信息,请参阅官方 API 参考。