angular中的ng-bind-html和$sce服务

输入的内容存储在数据库中,然后再在数据库中将这些数据读写到页面上,比如你使用了某个第三方的脚本或者库、加载了一段html等等,可能会多了一些css的样式(显示在界面上)

这个时候我们可以利用angular的$sce,ng-bind-html来进行数据的安全绑定。

js:
controller('transferWorkStep2', ['$scope','$http','$routeParams','$sce', function ($scope,$http, $routeParams, $sce) { $http.get('/api/work/get?workId=' + $routeParams.workId) .success(function (work) { $scope.currentWork = work; $scope.currentWork.description = $sce.trustAsHtml($rootScope.currentWork.description); });
html:
<p ng-bind-html="currentWork.description"></p>

 当然也可以将其封装成一个过滤器:

   

app.filter('to_trusted', ['$sce', function ($sce) {
return function (text) {
    return $sce.trustAsHtml(text);
};
<p ng-bind-html="currentWork.description | to_trusted"></p>  

 

参考链接:https://segmentfault.com/a/1190000000639561

                http://www.cnblogs.com/ys-ys/p/5001784.html

     http://www.cnblogs.com/xing901022/p/5100551.html

 

转载于:https://www.cnblogs.com/evaling/p/6795747.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值