angularjs加载html的指令是ng-bind-html
但加载前需要先将原始数据用$sce.trustAsHtml处理一下,在此记录一下以免遗忘
js代码如下
module.controller("MyController",["$scope","$sce"
,function($scope,$sce){
let test="<p>123</p>"
$scope.htmlText=$sce.trustAsHtml(test)
}])
html代码如下
<div ng-bind-html="htmlText">
</div>
也可以封装成为一个过滤器,这样使用起来更加方便!
module.filter('trustHtml',['$sce',function($sce){
return function(data){
return $sce.trustAsHtml(data);
}
}]);
<span ng-bind-html="testHtml|trustHtml"></span>