AngularJS转义显示html

ngSanitize

$sanitize

1.引入文件

    <script src="angular.js">
    <script src="angular-sanitize.js">

    angular.module('app', ['ngSanitize']);

2.Usage

    <!doctype html>
    <html ng-app="ngSanitize">
    <head>
    <script src="http://code.angularjs.org/1.2.2/angular.min.js"></script>
    <script src="script.js"></script>
    </head>
    <body>
    <div ng-controller="Ctrl">
    Snippet: <textarea ng-model="snippet" cols="60" rows="3"></textarea>
    <table>
    <tr>
    <td>Directive</td>
    <td>How</td>
    <td>Source</td>
    <td>Rendered</td>
    </tr>
    <tr id="bind-html-with-sanitize">
    <td>ng-bind-html</td>
    <td>Automatically uses $sanitize</td>
    <td><pre><div ng-bind-html="snippet"><br/></div></pre></td>
    <td><div ng-bind-html="snippet"></div></td>
    </tr>
    <tr id="bind-html-with-trust">
    <td>ng-bind-html</td>
    <td>Bypass $sanitize by explicitly trusting the dangerous value</td>
    <td>
    <pre><div ng-bind-html="deliberatelyTrustDangerousSnippet()">
    t;/div></pre>
    </td>
    <td><div ng-bind-html="deliberatelyTrustDangerousSnippet()"></div></td>
    </tr>
    <tr id="bind-default">
    <td>ng-bind</td>
    <td>Automatically escapes</td>
    <td><pre><div ng-bind="snippet"><br/></div></pre></td>
    <td><div ng-bind="snippet"></div></td>
    </tr>
    </table>
    </div>
    </body>
    </html>

    function Ctrl($scope, $sce) {
    $scope.snippet =
    '<p style="color:blue">an html\n' +
    '<em οnmοuseοver="this.textContent=\'PWN3D!\'">click here</em>\n' +
    'snippet</p>';
    $scope.deliberatelyTrustDangerousSnippet = function() {
    return $sce.trustAsHtml($scope.snippet);
    };
    }



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值