1、js中添加过滤器
//把后端返回的数据解析成HTML
bmainApp.filter('toHtml',['$sce',function($sce) {
return function(text) {
return $sce.trustAsHtml(text);
};
//把后端返回的数据解析成HTML后可以使用函数,处理返回数据中的函数不能用的问题:
bmainApp.directive('compileHtml', function ($compile) {
return {
restrict: 'A',
replace: true,
link: function (scope, ele, attrs) {
scope.$watch(function () {return scope.$eval(attrs.compile);},
function(html) {
ele.html(html);
$compile(ele.contents())(scope);
});
}
};
});
2、前端页面
<span ng-bind-html="good.jtype | toHtml compile-html"></span>
说明:good.jtype为封装的数据。
toHtml为过滤器名称。
compile-html解决函数不能使用。
3、后端代码封装
String str="<a href style='color: blue;' ng-click='refund(\"" + ids[j]
+ "\")' class='btn btn-primary btn-mini'>付款</a><br/>";
注意:封装是注意函数后面要用双引号不能用单引号
引用:
参考链接