一直在档案馆和网上寻找一个答案,但不是真正找到一个,只是点点滴滴.似乎有很多建议的帖子,但没有一个有答案.
我有一个复杂的指令,使用范围:true.它实际上是我正在尝试编写清理代码的ng-grid最新2.x版本,因为它只是像疯了一样泄漏内存,而我们的应用程序暂时"卡住"它.这是演示问题的插件.当您单击网格并随后检查堆快照时,您将看到几个"未附加的"ngGrid对象.这些仅由听众在指令的范围内压制.
当我通过单击多个网格(状态)更改状态(使用ui.route最新)时,网格指令的范围是获取$ destroy事件.处理程序正在运行.但是,范围本身并没有调用$ destroy().我在堆快照中看到指令的范围仍然通过$$监听器保留元素.此外,未设置范围.$$销毁.
但是,范围.原型 IS破坏.因为它是,我甚至不能从指令的$ on- $ destroy处理程序调用$ scope.$ destroy,因为proto的$ destroy()调用将$ destroy的定义更改为noop:
ngGridDirectives.directive('ngGrid', ['$compile', '$filter', '$templateCache', '$sortService', '$domUtilityService', '$utilityService', '$timeout', '$parse', '$http', '$q',
function($compile, $filter, $templateCache, sortService, domUtilityService, $utils, $timeout, $parse, $http, $q) {
var ngGridDirective = {
scope: true,
compile: function() {
return {
pre: function($scope, iElement, iAttrs) {
$(iElement).on('$destroy', function cleanupElementNgGrid() {
$timeout(function() {
$scope.$destroy();
$scope.destroyedByForce = true;
console.log("I destroyed it.");
},4000);
});
...
Run Code Online (Sandbox Code Playgroud)
设置范围.$$ listeners = {},$ timeout为2000(为指令完成清理我的on- $ destroy监听器的时间似乎有效,但是对内部进行挖掘感觉不对,有时它是不足以让浏览器完成清理工作.这只是一个解决方法/解决方案.
我也试过这个: …