Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot read property '$$nextSibling' of null

Tags:

angularjs

I keep getting this error in my program when I destroy my own scopes. I tracked it down to this while loop within angular:

if (!(next = (current.$$childHead || (current !== target && current.$$nextSibling)))) {
    while(current !== target && !(next = current.$$nextSibling)) {
        current = current.$parent;
    }
}

I've also managed to replicate it in a jsFiddle: http://jsfiddle.net/kEyqz/1/

It's pretty easy to set up, on an element I wire up a click event and then $broadcast that it was clicked followed by an $apply.

In a separate directive (with a child scope) I listen in on that event, and if I see it, I destroy the child scope. Doing so produces the error:

Uncaught TypeError: Cannot read property '$$nextSibling' of null 

This is just a small example of what I'm trying to accomplish in my code.

Console logging right before current = current.$parent results in the scope having no $parent (it is set to null).

I can add the lines:

if(!current){
    break;
}

After the current = current.$parent, but i'd have to maintain it if I switch angular versions (not ideal)

like image 840
Mathew Berg Avatar asked Sep 06 '13 15:09

Mathew Berg


1 Answers

Is this what you were looking for http://jsfiddle.net/jw99Lj7t/ ?

You were $destroy'ing the $scope durgin digest cycle - that was causing the error.

I don't belive that this is a bug since this is how digest cycle is implemented (it loops over the list of $scopes checking if something has changed, and we can't remove the scopes from that list as it is "already in use").

Resolving this issue, may introduce a lot of complexities and since digest must be very fast, it's better to just find a different way for the thing you are doing.

like image 127
g00fy Avatar answered Sep 21 '22 01:09

g00fy