简体   繁体   中英

ng-bind-html working, but throwing error

I'm using ng-bind-html to render sanitized comment HTML. This is my html:

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

It works - the HTML is rendered and displays correctly. But I get the following error in the Javascript console:

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">

This is the code that causes the problem in angular.js :

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

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

The bindings variable ends up being the string comment.Text , which is why it doesn't support the method push , because it's not an array.

What should I change to fix this?

You're right .push will only work with an array. Using this you have to initialize your variable

var bindings = new Array();

you might also try to consider using .pushStack => http://api.jquery.com/pushstack/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM