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

ng-bind-html working, but throwing error

本文关键字:出错 错误 工作 ng-bind-html      更新时间:2023-09-26

我正在使用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/

相关文章: