Skip to content

Commit 949525d

Browse files
IgorMinarjbdeboer
authored andcommitted
fix(Scope): ensure that a scope is destroyed only once
Due to bd524fc calling $destroy() on a scope mupltiple times cases NPE. Closes angular#1627
1 parent adaa659 commit 949525d

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/ng/rootScope.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ function $RootScopeProvider(){
134134
this.$$nextSibling = this.$$prevSibling =
135135
this.$$childHead = this.$$childTail = null;
136136
this['this'] = this.$root = this;
137+
this.$$destroyed = false;
137138
this.$$asyncQueue = [];
138139
this.$$listeners = {};
139140
}
@@ -467,10 +468,12 @@ function $RootScopeProvider(){
467468
* perform any necessary cleanup.
468469
*/
469470
$destroy: function() {
470-
if ($rootScope == this) return; // we can't remove the root node;
471+
// we can't destroy the root scope or a scope that has been already destroyed
472+
if ($rootScope == this || this.$$destroyed) return;
471473
var parent = this.$parent;
472474

473475
this.$broadcast('$destroy');
476+
this.$$destroyed = true;
474477

475478
if (parent.$$childHead == this) parent.$$childHead = this.$$nextSibling;
476479
if (parent.$$childTail == this) parent.$$childTail = this.$$prevSibling;

test/ng/rootScopeSpec.js

+16
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,22 @@ describe('Scope', function() {
407407
first.$destroy();
408408
expect(log).toEqual('first; first-child');
409409
}));
410+
411+
412+
it('should $destroy a scope only once and ignore any further destroy calls',
413+
inject(function($rootScope) {
414+
$rootScope.$digest();
415+
expect(log).toBe('123');
416+
417+
first.$destroy();
418+
first.$apply();
419+
expect(log).toBe('12323');
420+
421+
first.$destroy();
422+
first.$destroy();
423+
first.$apply();
424+
expect(log).toBe('1232323');
425+
}));
410426
});
411427

412428

0 commit comments

Comments
 (0)