How do i decode HTML entity in text using angular JS.
如何使用angular JS解码文本中的HTML实体。
I have the string
我有字符串
""12.10 On-Going Submission of ""Made Up"" Samples.""
I need a way to decode this using Angular JS. I found a way to do that using javascript here but I am sure thats wont work for Angular. Need to get back the original string on the UI which would look like
我需要一种使用Angular JS解码的方法。我在这里找到了一种使用javascript的方法,但我相信它不适用于Angular。需要在UI上找回原始字符串,看起来像
""12.10 On-Going Submission of ""Made Up"" Samples.""
3 个解决方案
#1
41
You can use the ng-bind-html
directive to display it as an html content with all the html entities decoded. Just make sure to include the ngSanitize
dependency in your application.
您可以使用ng-bind-html指令将其显示为html内容,并解码所有html实体。只需确保在应用程序中包含ngSanitize依赖项。
DEMO
JAVASCRIPT
JAVASCRIPT
angular.module('app', ['ngSanitize'])
.controller('Ctrl', function($scope) {
$scope.html = '"12.10 On-Going Submission of ""Made Up"" Samples."';
});
HTML
HTML
<body ng-controller="Ctrl">
<div ng-bind-html="html"></div>
</body>
#2
23
If you don't want to use ngSanitize, you can do it this way:
如果您不想使用ngSanitize,可以这样做:
in your controller:
在你的控制器中:
$scope.html = '"12.10 On-Going Submission of ""Made Up"" Samples."'
$scope.renderHTML = function(html_code)
{
var decoded = angular.element('<textarea />').html(html_code).text();
return $sce.trustAsHtml(decoded);
};
And in the template:
在模板中:
<div ng-bind-html="renderHTML(html)"></div>
Just make sure you inject $sce in your controller
只需确保在控制器中注入$ sce
#3
1
I have similar issue, but don't need to use result value on UI. This issue was resolved by code from angular ngSanitize module:
我有类似的问题,但不需要在UI上使用结果值。此问题已通过角度ngSanitize模块中的代码解决:
var hiddenPre=document.createElement("pre");
/**
* decodes all entities into regular string
* @param value
* @returns {string} A string with decoded entities.
*/
function decodeEntities(value) {
if (!value) { return ''; }
hiddenPre.innerHTML = value.replace(/</g,"<");
// innerText depends on styling as it doesn't display hidden elements.
// Therefore, it's better to use textContent not to cause unnecessary reflows.
return hiddenPre.textContent;
}
var encoded = '<p>name</p><p><span style="font-size:xx-small;">ajde</span></p><p><em>da</em></p>';
var decoded = decodeEntities(encoded);
document.getElementById("encoded").innerText=encoded;
document.getElementById("decoded").innerText=decoded;
#encoded {
color: green;
}
#decoded {
color: red;
}
Encoded: <br/>
<div id="encoded">
</div>
<br/>
<br/>
Decoded: <br/>
<div id="decoded">
</div>
#1
41
You can use the ng-bind-html
directive to display it as an html content with all the html entities decoded. Just make sure to include the ngSanitize
dependency in your application.
您可以使用ng-bind-html指令将其显示为html内容,并解码所有html实体。只需确保在应用程序中包含ngSanitize依赖项。
DEMO
JAVASCRIPT
JAVASCRIPT
angular.module('app', ['ngSanitize'])
.controller('Ctrl', function($scope) {
$scope.html = '"12.10 On-Going Submission of ""Made Up"" Samples."';
});
HTML
HTML
<body ng-controller="Ctrl">
<div ng-bind-html="html"></div>
</body>
#2
23
If you don't want to use ngSanitize, you can do it this way:
如果您不想使用ngSanitize,可以这样做:
in your controller:
在你的控制器中:
$scope.html = '"12.10 On-Going Submission of ""Made Up"" Samples."'
$scope.renderHTML = function(html_code)
{
var decoded = angular.element('<textarea />').html(html_code).text();
return $sce.trustAsHtml(decoded);
};
And in the template:
在模板中:
<div ng-bind-html="renderHTML(html)"></div>
Just make sure you inject $sce in your controller
只需确保在控制器中注入$ sce
#3
1
I have similar issue, but don't need to use result value on UI. This issue was resolved by code from angular ngSanitize module:
我有类似的问题,但不需要在UI上使用结果值。此问题已通过角度ngSanitize模块中的代码解决:
var hiddenPre=document.createElement("pre");
/**
* decodes all entities into regular string
* @param value
* @returns {string} A string with decoded entities.
*/
function decodeEntities(value) {
if (!value) { return ''; }
hiddenPre.innerHTML = value.replace(/</g,"<");
// innerText depends on styling as it doesn't display hidden elements.
// Therefore, it's better to use textContent not to cause unnecessary reflows.
return hiddenPre.textContent;
}
var encoded = '<p>name</p><p><span style="font-size:xx-small;">ajde</span></p><p><em>da</em></p>';
var decoded = decodeEntities(encoded);
document.getElementById("encoded").innerText=encoded;
document.getElementById("decoded").innerText=decoded;
#encoded {
color: green;
}
#decoded {
color: red;
}
Encoded: <br/>
<div id="encoded">
</div>
<br/>
<br/>
Decoded: <br/>
<div id="decoded">
</div>