Cannot read property '$$nextSibling' of null


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:

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:


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

Problem courtesy of: Mathew Berg


Is this what you were looking for ?

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.

Solution courtesy of: g00fy


There is currently no discussion for this recipe.

This recipe can be found in it's original form on Stack Over Flow.