Skip to content

Commit afdc34e

Browse files
committed
Fixed opening for memory leak bug using deregister method on service and scope.$on('$destroy') in pagination link function
1 parent f2997ae commit afdc34e

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/directives/pagination/dirPagination.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@
6262
// Now that we have access to the `scope` we can interpolate any expression given in the paginationId attribute and
6363
// potentially register a new ID if it evaluates to a different value than the rawId.
6464
var paginationId = $parse(attrs.paginationId)(scope) || attrs.paginationId || DEFAULT_ID;
65+
// In case rawId != paginationId we deregister using rawId for the sake of general cleanliness
66+
// before registering using paginationId
67+
paginationService.deregisterInstance(rawId);
6568
paginationService.registerInstance(paginationId);
6669

6770
var repeatExpression;
@@ -115,6 +118,12 @@
115118

116119
// Delegate to the link function returned by the new compilation of the ng-repeat
117120
compiled(scope);
121+
122+
// When the scope is destroyed, we make sure to remove the reference to it in paginationService
123+
// so that it can be properly garbage collected
124+
scope.$on('$destroy', function destroyDirPagination() {
125+
paginationService.deregisterInstance(paginationId);
126+
});
118127
};
119128
}
120129
};
@@ -351,6 +360,10 @@
351360
}
352361
};
353362

363+
this.deregisterInstance = function(instanceId) {
364+
delete instances[instanceId];
365+
};
366+
354367
this.isRegistered = function(instanceId) {
355368
return (typeof instances[instanceId] !== 'undefined');
356369
};
@@ -410,4 +423,4 @@
410423
};
411424
};
412425
});
413-
})();
426+
})();

0 commit comments

Comments
 (0)