zoukankan      html  css  js  c++  java
  • angularjs 可以加入html标签方法------ng-bind-html的用法总结(1)

    本篇主要讲解angular中的$sanitize这个服务.此服务依赖于ngSanitize模块.(这个模块需要加载angular-sanitize.js插件)

    要学习这个服务,先要了解另一个指令: ng-bing-html.

    顾名思义,ng-bind-html和ng-bind的区别就是,ng-bind把值作为字符串,和元素的内容进行绑定,但是ng-bind-html把值作为html,和元素的html进行绑定.相当于jq里面的.text()和.html().

    但是,出于安全考虑,如果我们直接使用ng-bind-html是会报错的,ng-bind-html后面的内容必须经过一定的处理.

    处理的方式有两种,一种是使用$sce服务,另一种就是使用$sanitize服务.$sce服务怎么用,在以后的文章中会独立讲解,这篇主要讲解$sanitize服务.

    $sanitize会根绝一个白名单来净化html标签.这样,不安全的内容就不会被返回. 白名单是根据$compileProvider的aHrefSanitizationWhitelist和imgSrcSanitizationWhitelist函数得到的. 

    看一个栗子:

    <!DOCTYPE html>
    <html ng-app="myApp">
    <head>
        <title></title>
        <meta charset="utf-8">
        <script src="../angular-1.3.2.js"></script>
        <script src="angular-sanitize.min.js"></script>
        <script src="script.js"></script>
        <link type="text/css" href="../bootstrap.css" rel="stylesheet" />
    </head>
    <body>
    <div class="container">
        <table class="table table-bordered" ng-controller="ctrl">
            <thead>
            </thead>
            <tbody>
            <tr>
                <td>ng-bind-html</td>
                <td>使用内置的$sanitize <br/>(不需要出现在js里,只要模型添加了ngSanitize模块, <br/>然后使用ng-bind-html,它的值就自动通过$sanitize编译)</td>
                <td><div ng-bind-html="myHtml"></div></td>
            </tr>
            <tr>
                <td>ng-bind-html</td>
                <td>使用$sce的trustAsHtml方法编译<br/>(以后会细讲$sce服务,这里不是重点)</td>
                <td><div ng-bind-html="trustHtml"></div></td>
            </tr>
            <tr>
                <td>ng-bind</td>
                <td>不编译</td>
                <td><div ng-bind="myHtml"></div></td>
            </tr>
            </tbody>
        </table>
    
    
    </div>
    </body>
    </html>

  • 相关阅读:
    Error: unable to load xmlsec-openssl library
    count(1)、count(*)与count(列名)的执行区别
    Linux下的压缩zip,解压缩unzip命令详解及实例
    linux centos 如何查看操作系统版本信息
    These dependencies were not found: *!!vue-style-loader!css-loader?
    Git如何永久删除某个重要文件文件或文件夹 (包括历史记录) 强制
    LDAP的filter查询详解
    详谈mysqldump数据导出的问题
    GO -- 遍历删除 数组 slice
    mjml强大&&灵活的邮件模版引擎
  • 原文地址:https://www.cnblogs.com/qshting/p/5865654.html
Copyright © 2011-2022 走看看