AngularJs:如何去code HTML实体HTML?实体、何去、AngularJs、HTML

2023-09-13 05:20:40 作者:伴我多久@

局势:

我这bulding的内容是采取调用一​​个JSON格式返回数据的API的网页。

I am bulding a webpage which content is taken calling an API that returns the data in json format.

问题是,HTML标记给定为HTML实体,一个必须去codeD。

The problem is that the html tags are given as HTML entities, that has to be decoded.

示例:

这是例子我处理的JSON的:

This is example of the json i am dealing with:

<p align="justify"><strong>15<sup>th</sup> HERE THERE IS A BOLD TEXT </strong> HERE SOME NORMAL TEXT...

ATTEMPT:

我花时间研究它,它似乎比我想象更难。展望谷歌和类似SO质疑,一个可能的解决方案是使用NG-炳HTML

I have spend time research it and it seems harder than i thought. Looking in google and similar SO question, a possible solution is to use the ng-bing-html

API调用:

$http.get('http://API/page_content').then(function(resp) 
{
    $scope.content_test = resp.data[0].content; 
}

过滤器:

.filter('trusted', ['$sce', function($sce){
    return function(text) {
        return $sce.trustAsHtml(text);
    };
}])

伍绑定-HTML的角度认为:

Ng-bind-html in the angular view:

<div ng-bind-html=" content_test  | trusted"></div>

OUTPUT:

这是在视图中的输出(完全按照您看到它):

This is the output in the view (exactly as you see it):

<p align="justify"><strong>15<sup>th<\/sup> HERE THERE IS A BOLD TEXT<\/strong> HERE SOME NORMAL TEXT...

但我需要看到的是正确格式的文本:

but what i need to see is the text properly formatted:

这里还有一个大胆的文字这里是一些普通的文本...

HERE THERE IS A BOLD TEXT HERE SOME NORMAL TEXT...

问题:

如何去在AngularJs适当格式化的HTML code HTML实体?

How can i decode HTML entities in a proper formatted HTML in AngularJs?

推荐答案

我想你需要执行一个更多的解码步骤中,您将它传递给 $ SCE 之前。例如像这样的:

I think you need to perform one more "decoding" step before you pass it to $sce. For example like this:

app.filter('trusted', ['$sce', function($sce) {
    var div = document.createElement('div');
    return function(text) {
        div.innerHTML = text;
        return $sce.trustAsHtml(div.textContent);
    };
}]);

演示: http://plnkr.co/edit/LrT4tgYtTu4CPrOAidra?p=$p$pview