Angularjs: TypeError: Cannot call method 'insertBefore' of null

前端 未结 5 607
天命终不由人
天命终不由人 2021-02-05 14:49

Please find the fiddle here http://jsfiddle.net/UxYLa/6/

This is a simplified form of what I am trying to do. There are two directive, and the nested one, subDirec

5条回答
  •  -上瘾入骨i
    2021-02-05 14:55

    It has something to do with the compilation and how data is bound. I made a working version here: http://jsfiddle.net/UxYLa/9/

    The main differences are in b.html

      

    Have to interate over arrays and refer to itm instead of model since it's in the scope.

    Edit: after some digging here are some more clues as to why the error happens, but not when you wrap it in a div. It is calling this: parent.insertBefore(node, index.nextSibling); where parent is element.parent of your ng-repeat. If you don't have the wrapper, the parent is null.

    This means the error occurs whenever the html you are compiling has a watch that is on the outside of the template when directly changing the element.

    I also made a solution that doesn't attempt to change element directly, but rather appends the compiled element to it. So that everything, when it's checked in the digest cycle will have a proper structure. http://jsfiddle.net/UxYLa/12/

    Hope this helped.

提交回复
热议问题