博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ng-bind-html 的使用
阅读量:6950 次
发布时间:2019-06-27

本文共 2876 字,大约阅读时间需要 9 分钟。

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

...

测试案例:

index.html

index.js

var myApp = angular.module('myApp', []);myApp.controller('TestCtrl', ['$scope', function($scope){        $scope.myHTML = 'Hot';}]);

浏览器输出下面错误:

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加载进来就可以了。

 

转载地址:http://wskil.baihongyu.com/

你可能感兴趣的文章
Makefile学习与进阶之Makefile.am和$$(M)的意思
查看>>
Codeforces Round #382 (Div. 2)
查看>>
日历控件--My97DatePicker的使用
查看>>
k8s运行容器之Job(四)--技术流ken
查看>>
Android下打印调试堆栈方法(转)
查看>>
iOS7坐标上移44pt的解决
查看>>
面向对象基础
查看>>
Python全栈开发—第1站
查看>>
15 函数回调 模块
查看>>
iOS掉落回弹效果
查看>>
Eclipse--eclipse去除js(JavaScript)验证错误
查看>>
mac关闭和开启启动声
查看>>
浅谈WebService开发(一)
查看>>
学习Zookeeper之第2章Zookeeper安装
查看>>
java开始到熟悉100-102
查看>>
(译)我为什么用Go语言来做区块链——Syed Jafar Naqvi——Co-Founder/CEO at Karachain...
查看>>
随机生成一个不重复的身份码,包含数字和字母
查看>>
王彪-20162321-实验二 树
查看>>
HDU 1754 线段树裸题
查看>>
异常处理
查看>>