Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/463.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 字符串未在angularJs中使用$sce呈现为html_Javascript_Html_Angularjs - Fatal编程技术网

Javascript 字符串未在angularJs中使用$sce呈现为html

Javascript 字符串未在angularJs中使用$sce呈现为html,javascript,html,angularjs,Javascript,Html,Angularjs,我正在从服务器text获取以下数据 我正在控制器中使用以下功能 $scope.getHtml = function (html) { return $sce.trustAsHtml(html); }; 在html中,如下所示 <div class="col-sm-12 col-md-12" ng-bind-html="getHtml(vm.profileData.htmltext)"> 执行此操作后,我将看到它,但它不会渲染: <span>text<

我正在从服务器
text
获取以下数据

我正在控制器中使用以下功能

$scope.getHtml = function (html) {
    return $sce.trustAsHtml(html);
};
在html中,如下所示

<div class="col-sm-12 col-md-12" ng-bind-html="getHtml(vm.profileData.htmltext)">

执行此操作后,我将看到它,但它不会渲染:

<span>text</span>
文本

请告诉我哪里出了问题?提前感谢

为此,我推荐此代码

function htmlDecode(str) {
     return $('<textarea />').html(str).text();   
}  

// angular js
$sce.trustAsHtml(htmlDecode(html));
函数htmlDecode(str){
返回$('').html(str.text();
}  
//角js
$sce.trustAsHtml(htmlDecode(html));

正如我提到的,您需要一个html实体解码

 $scope.html = angular.element('<div></div>').html('&lt;i&gt;text&lt;/i&gt;').text();
 $scope.getHtml = function() {
  return $sce.trustAsHtml($scope.html);
};
$scope.html=angular.element(“”).html('itext/i').text();
$scope.getHtml=function(){
返回$sce.trustAsHtml($scope.html);
};

如何设置
vm.profileData.htmltext
?您能分享更多的代码吗?我从服务器获取的数据如下所示,并在vm.profileData.htmltext中设置。vm.profileData.htmltext='spantext/span'可能是您应该在分配给html文本之前进行html实体解码。我不确定您是否可以为$sce.trustAsHtml函数编写tryPost。这是angular sanitize的内置函数,$sce Thank you.。它起作用了。。但是我没有声誉去投票。。对不起……)