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. poj2068--Nim

    题意:给你2n个人,两方各n个人,交叉坐,每个人可以取的石子有一个最大限制,总共有S颗石子,哪一方取了最后一颗石子就输了,问先取石子的这一方是否有必胜策略. DP,dp[i][j]代表第i个人还有J个 ...

  2. python编程之处理GB级的大型文件

    一般我们采取分块处理,一次处理固定大小的块. def read_in_chunks(file_obj,chunk_size): """Lazy function (gen ...

  3. 把第三方的exe程序嵌入C#界面上

    public partial class eTerm_Form : WinFormsUI.Docking.DockContent{public eTerm_Form(){InitializeCompo ...

  4. dos判断系统版本的语句,

    是我在单位批量安装软件使用的批处理,判断方法如下,仅供参考,本人不懂任何编程,大家凑合着看: winxp和win7判断: net config workstation |(find /i " ...

  5. joomla安装插件报错:上传文件到服务器发生了一个错误。 过小的PHP文件上传尺寸

    在安装joomla的AKeeba插件的时候报错如下:上传文件到服务器发生了一个错误. 过小的PHP文件上传尺寸.解决方法是修改php.ini文件,打开文件后搜索upload_max_filesize! ...

  6. javascript笔记5之流程控制语句

    /* var box = 100; //单行语句 var age = 20; //另一条单行语句 { //用花括号包含的语句集合,叫做复合语句,单位一个 //一对花括号,表示一个复合语句,处理时候,可 ...

  7. C# - List操作- 去掉重复

    ChangeList里面会有重复的数据,这时可以这样去掉重复的item // Remove duplicated info var dup = ChangeList.Where(item => ...

  8. 真实经纬度(gps)转成百度坐标的js方法

    转:http://www.360doc.com/content/16/0320/14/18636294_543805051.shtml 结果图: <!DOCTYPE html> <h ...

  9. Hive MapJoin

    摘要 MapJoin是Hive的一种优化操作,其适用于小表JOIN大表的场景,由于表的JOIN操作是在Map端且在内存进行的,所以其并不需要启动Reduce任务也就不需要经过shuffle阶段,从而能 ...

  10. HDU 2845 Beans (DP)

    Problem Description Bean-eating is an interesting game, everyone owns an M*N matrix, which is filled ...