javascript - ng-bind-html - 使用 angular-sanitize - 在控制台中抛出错误

标签 javascript angularjs angular-ui

我正在尝试将 html 添加到 div,如下所示:

$scope.thehtml = $sce.trustAsHtml("<b>hello</b>")

<div ng-bind-html="thehtml">
</div>

我在控制台中看到以下错误:

TypeError: undefined is not a function
    at htmlParser (js/angular/angular-sanitize.js:205:17)
    at $sanitize (js/angular/angular-sanitize.js:119:5)
    at Object.ngBindHtmlWatchAction [as fn] (js/angular/angular-sanitize.js:420:15)
    at h.$get.h.$digest (js/angular/angular.min-125.js:98:396)

angular-sanitize.js:205:17 是这样的:

if ( html.indexOf("<!--") === 0 ) {
index = html.indexOf("-->");

显然 html 没有被视为字符串,因此 indexOf 上的错误?

我已经包含了 angular-sanitize.js,并向应用程序添加了模块“ngSanitize”。

最佳答案

请看这个plunk . 据我所知,您的实现看起来不错!我的 plunk 正在工作,我没有收到相同代码的异常。 我不必将 ngSanitize 添加为模块的依赖项,我只是将 $sce 注入(inject)到我的 Controller 中。

我用过的库版本如下:

https://code.angularjs.org/1.2.28/angular.js
https://code.angularjs.org/1.2.0-rc.3/angular-sanitize.js

请尝试用这些版本替换您的脚本引用,看看它是否是有问题的库。

告诉我你过得怎么样!祝你好运:)

关于javascript - ng-bind-html - 使用 angular-sanitize - 在控制台中抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26388875/

相关文章:

javascript - 我如何 JSON.parse 来自 URL 的字符串(React Native)

html - 页脚应该在页面/内容的底部

angularjs - 使用AngularJS ui-grid $ scope.saveRow保存行数据

javascript - javascript书签代码中 'scr' +'ipt'的串联

javascript - 上传多张图片上传前预览图片

javascript - 如何检查 on{x} 中的 "device.calendar.ongoing"中的内容?

css - 在 Angularjs 中为自定义复选框创建自定义指令

javascript - D3 水平条形图 X 轴范围比 AngularJs 数据中的范围短

javascript - Angular Bootstrap 选项卡 - 选择页面加载中调用的函数

javascript - Angular-ui-bootstrap datepicker 指令 - 放在前面