AngualrJS 提供了指令ng-bind-html 用于绑定包含HTML标签的文档,使用方式:

<ANY
ng-bind-html="">
...
</ANY>

测试案例:

index.html

<div ng-controller="TestCtrl">
<div>
<p ng-bind-html="myHTML"></p>
</div>
</div>

index.js

var myApp = angular.module('myApp', []);

myApp.controller('TestCtrl', ['$scope', function($scope){
$scope.myHTML = '<strong>Hot</strong>';
}]);

浏览器输出下面错误:

angular.js:11598 Error: [$sce:unsafe] Attempting to use an unsafe value in a safe context.
http://errors.angularjs.org/1.3.11/$sce/unsafe
at file:///home/y/my_temp/angular_test/web/app/js/angular.js:63:12
at htmlSanitizer (file:///home/y/my_temp/angular_test/web/app/js/angular.js:15053:13)
at getTrusted (file:///home/y/my_temp/angular_test/web/app/js/angular.js:15217:16)
at Object.sce.(anonymous function) [as getTrustedHtml] (file:///home/y/my_temp/angular_test/web/app/js/angular.js:15897:16)
at Object.ngBindHtmlWatchAction [as fn] (file:///home/y/my_temp/angular_test/web/app/js/angular.js:20449:29)
at Scope.$digest (file:///home/y/my_temp/angular_test/web/app/js/angular.js:14230:29)
at Scope.$apply (file:///home/y/my_temp/angular_test/web/app/js/angular.js:14493:24)
at bootstrapApply (file:///home/y/my_temp/angular_test/web/app/js/angular.js:1449:15)
at Object.invoke (file:///home/y/my_temp/angular_test/web/app/js/angular.js:4182:17)
at doBootstrap (file:///home/y/my_temp/angular_test/web/app/js/angular.js:1447:14)angular.js:11598 (anonymous function)angular.js:8548 (anonymous function)

查阅官方文档要使用:$sanitize服务

Note: If a $sanitize service is unavailable and the bound value isn't explicitly trusted, you will have an exception (instead of an exploit.)

在angular.module中配置sanitize服务:

var myApp = angular.module('myApp', ['ngSanitize']);

再次刷新浏览器,输出以下错误信息:

 Uncaught Error: [$injector:modulerr] Failed to instantiate module myApp due to:
Error: [$injector:modulerr] Failed to instantiate module ngSanitize due to:
Error: [$injector:nomod] Module 'ngSanitize' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
http://errors.angularjs.org/1.3.11/$injector/nomod?p0=ngSanitize
at file:///home/y/my_temp/angular_test/web/app/js/angular.js:63:12
at file:///home/y/my_temp/angular_test/web/app/js/angular.js:1764:17
at ensure (file:///home/y/my_temp/angular_test/web/app/js/angular.js:1688:38)
at module (file:///home/y/my_temp/angular_test/web/app/js/angular.js:1762:14)
at file:///home/y/my_temp/angular_test/web/app/js/angular.js:4094:22
at forEach (file:///home/y/my_temp/angular_test/web/app/js/angular.js:323:20)
at loadModules (file:///home/y/my_temp/angular_test/web/app/js/angular.js:4078:5)
at file:///home/y/my_temp/angular_test/web/app/js/angular.js:4095:40
at forEach (file:///home/y/my_temp/angular_test/web/app/js/angu

发现angular.js没有sanitize模块,在这里将

angular-sanitize.js加载进来就可以了。
<script src="../js/angular-sanitize.js"></script>

ng-bind-html 的使用的更多相关文章

  1. AngularJS进阶学习

    参考:http://***/class/54f3ba65e564e50cfccbad4b 1. AJAX:Asynchronous JavaScript and XML(异步的 JavaScript ...

  2. 【转载】AngularJs 指令directive之controller,link,compile

    关于自定义指令的命名,你可以随便怎么起名字都行,官方是推荐用[命名空间-指令名称]这样的方式,像ng-controller.不过你可千万不要用 ng-前缀了,防止与系统自带的指令重名.另外一个需知道的 ...

  3. angular(常识)

    我觉得angularjs是前端框架,而jquery只是前端工具,这两个还是没有可比性的. 看知乎上关于jquery和angular的对比http://www.zhihu.com/question/27 ...

  4. AngularJS开发指南4:指令的详解

    指令是我们用来扩展浏览器能力的技术之一.在DOM编译期间,和HTML元素关联着的指令会被检测到,并且被执行.这使得指令可以为DOM指定行为,或者改变它. AngularJS有一套完整的.可扩展的.用来 ...

  5. angularjs 指令(directive)详解(1)

    原文地址 什么是directive?我们先来看一下官方的解释: At a high level, directives are markers on a DOM element (such as an ...

  6. AngularJs学习笔记--directive

    原版地址:http://code.angularjs.org/1.0.2/docs/guide/directive Directive是教HTML玩一些新把戏的途径.在DOM编译期间,directiv ...

  7. Directive Definition Object

    不知道为什么这个我并没有想翻译过来的欲望,或许我并没有都看熟透,不好误人子弟,原版奉上. Here's an example directive declared with a Directive D ...

  8. AngularJS的启动引导过程

    原文:http://www.angularjs.cn/A137?utm_source=ourjs.com 目录: 引导之前 自动引导启动框架 手工引导启动框架 引导第1步:创建注入器 引导第2步:创建 ...

  9. Angular指令(一)

    Angular开发者手册重点翻译之指令(一) 创建自定义的指令 这个文章将解释什么需要在自己的angularjs应用中创建自己的指令,以及如何实现它. 什么是指令 在高的层面上讲,指令是DOM元素中的 ...

  10. Scope Directive

    ---------------------------Scope-------------------------------- https://docs.angularjs.org/guide/sc ...

随机推荐

  1. 怎样检查手机是否root成功

    怎样检查手机是否root成功 浏览:154361 | 更新:2011-01-20 13:10 | 标签:root 总有人以为,root后就可以删除自带程序了,这个想法也对也不对,想删除自带的软件,确实 ...

  2. dedecms 在php7.0无法安装

    dedecms 需要mysql扩展的支持!而php7.0已废弃mysql扩展.所以我讲7.0改回了5.6然后就可以顺利安装了. 总结了一个经验:没有绝对实力,不要尝试新东西

  3. allVncClients

    VNC Viewer Free Edition 37  RealVNC Ltd.  15,367  Freeware  1021.58 KB VNC is client and server remo ...

  4. PyCharm 4.0下载(附keygen)

    百度网盘:http://pan.baidu.com/s/1nvAdEM9  密码:xfz9

  5. http接口测试浏览器插件

    http接口测试浏览器插件: Chrome: https://chrome.google.com/webstore/detail/chrome-poster/cdjfedloinmbppobahmon ...

  6. MVVM架构~knockoutjs系列之表单添加(验证)与列表操作源码开放

    返回目录 本文章应该是knockoutjs系列的最后一篇了,前几篇中主要讲一些基础知识,这一讲主要是一个实际的例子,对于一个对象的添加与编辑功能的实现,并将项目源代码公开了,共大家一起学习! knoc ...

  7. python scrapy 基础

    scrapy是用python写的一个库,使用它可以方便的抓取网页. 主页地址http://scrapy.org/ 文档 http://doc.scrapy.org/en/latest/index.ht ...

  8. spark1.1.0学习路线

          经过一段时间授课,积累下不少的spark知识.想逐步汇总成资料,分享给小伙伴们.对于想视频学习的小伙伴,能够訪问炼数成金站点的<spark大数据平台>课程.每周的课程是原理加实 ...

  9. Codeforces Round #311 (Div. 2) E - Ann and Half-Palindrome(字典树+dp)

    E. Ann and Half-Palindrome time limit per test 1.5 seconds memory limit per test 512 megabytes input ...

  10. Hacker(23)----破解常见文件密码

    Win7中,office文档.压缩文件等都是常见的文件,这些文档含有重要的信息,即使用户为这些文件设置了密码,黑客也会有办法破解. 一.破解office文档密码 破解office文档密码常用工具是Ad ...