我正在尝试将 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/