javascript - ng-bind-html 工作,但抛出错误

标签 javascript html angularjs

我正在使用 ng-bind-html 呈现净化后的评论 HTML。这是我的 html:

<span class="commentBody" ng-bind-html="comment.Text"></span>

有效 - HTML 已正确呈现和显示。但是我在 Javascript 控制台中收到以下错误:

TypeError: Object doesn't support property or method 'push'
   at $$addBindingInfo (http://localhost:2239/Scripts/angular.js:6869:9)
   at ngBindHtmlLink (http://localhost:2239/Scripts/angular.js:20460:9)
   at invokeLinkFn (http://localhost:2239/Scripts/angular.js:8219:9)
   at nodeLinkFn (http://localhost:2239/Scripts/angular.js:7729:11)
   at compositeLinkFn (http://localhost:2239/Scripts/angular.js:7078:13)
   at compositeLinkFn (http://localhost:2239/Scripts/angular.js:7081:13)
   at publicLinkFn (http://localhost:2239/Scripts/angular.js:6957:30)
   at boundTranscludeFn (http://localhost:2239/Scripts/angular.js:7096:9)
   at controllersBoundTransclude (http://localhost:2239/Scripts/angular.js:7756:11)
   at ngRepeatAction (http://localhost:2239/Scripts/angular.js:24553:15) <span class="commentBody ng-binding" ng-bind-html="comment.Text">

这是导致 angular.js 问题的代码:

  var bindings = $element.data('$binding') || [];

  if (isArray(binding)) {
    bindings = bindings.concat(binding);
  } else {
    bindings.push(binding);
  }

bindings 变量最终成为字符串 comment.Text,这就是它不支持方法 push 的原因,因为它是不是数组。

我应该更改什么来解决这个问题?

最佳答案

你是对的。push 只适用于数组。使用这个你必须初始化你的变量

var bindings = new Array();

您也可以尝试考虑使用 .pushStack => http://api.jquery.com/pushstack/

关于javascript - ng-bind-html 工作,但抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29048594/

相关文章:

javascript - jQuery 获取所有 <the> 值的数组

javascript - ReactJS 动态替换 child

javascript - 延迟升级到 HTTPS (TLS/SSL)

javascript - 使用 AngularJS 和 Lodash 将数组转换为字符串

javascript - 光泽热图 Highcharts

javascript - ReactJS 使用 getElementById 与库集成

javascript - 使用事件 "onclick"更改图像 src

javascript - 覆盖可见的内部div

javascript - 参数 ... 不是函数,未定义

angularjs - es5-shim 或 chrome frame 使 Angular 应用程序在 IE 中工作