【发布时间】:2018-05-31 11:40:45
【问题描述】:
我正在尝试通过使用 ng-bind-html 和过滤器来显示我在 div.testData 中收到的 html 内容。 我已经在我的应用程序中包含了“ngSanitize”。 但不知何故,它只能部分起作用。好像,过滤器没有被应用。 当我创建一个本地文件并检查时它工作正常,但当我在我的项目环境中使用相同的代码时它不起作用。
样本数据:
$scope.userProfile.Information = '<p>Hello, this is sample data to test html.</p>';
显示的输出是:
'<p>Hello, this is sample data to test html.</p>'
期望的输出是:
'Hello, this is sample data to test html.'
请帮我解决这个问题。
HTML:
<div ng-app="myApp" ng-controller="myCtrl">
<div class="testData" ng-bind-html= "userProfile.Information | to_trusted"></div>
过滤器:
app.filter('to_trusted', ['$sce', function($sce){
return function(text) {
var doc = new DOMParser().parseFromString(text, "text/html");
var rval= doc.documentElement.textContent;
//console.log(rval);
return $sce.trustAsHtml(rval);
};
}]);
【问题讨论】:
标签: angularjs parsing filter ng-bind-html ngsanitize