我想要动态创建使用javascript的角度组件,然后使用$compile
使用新创建的作用域进行角度编译。然后,当我不再使用该组件时,我想销毁组件和新范围。
一切都按预期工作,除了这个事实,即使我摧毁新范围,它使用的所有内存都不会被释放。
下面是该代码的简化版本的一部分:
这段代码的app.controller("mainCtrl", ["$scope", "$compile", function($scope, $compile) {
var childScope;
//call this every time the button is clicked
this.createDirective = function() {
//dynamically create a new instance of the custom directive
var customDirective = document.createElement("custom-directive");
//if another child scope exists, destroy it
if (childScope) {
childScope.$destroy();
childScope = undefined;
}
//create a new child scope
childScope = $scope.$new();
//compile the custom directive
$compile(customDirective)(childScope);
};
}]);
全部工作的例子是here
所有这些代码所做的,就是创建一个新的组件每一个按钮被点击时,但首先销毁已经存在的任何组件。 请注意,我实际上并没有在页面中添加已编译的组件,因为我注意到泄漏仍然存在,无论我是否使用它。
使用Chrome的开发工具(配置文件 - >录制分配时间轴 - >启动),我看到下面的内存使用情况单击按钮 几次后:
显然,任何占用内存由customDirective决不会实际发布,即使范例的$destroy
函数被调用。
我已经在过去成功地使用了$compile
而没有创建新的作用域,但似乎我在这种情况下丢失了一些东西。我是否还应该做其他事情以确保没有对新范围的引用?
编辑
基于低于JoelCDoyle答案,这里是修复(我添加的破坏功能,我创建范围):
app.controller("mainCtrl", ["$scope", "$compile", function($scope, $compile) {
var childScope;
//call this every time the button is clicked
this.createDirective = function() {
//dynamically create a new instance of the custom directive
var customDirective = document.createElement("custom-directive");
//if another child scope exists, destroy it
if (childScope) {
childScope.$destroy();
childScope = undefined;
}
//create a new child scope
childScope = $scope.$new();
//compile the custom directive
var compiledElement = $compile(customDirective)(childScope);
//FIX: remove the angular element
childScope.$on("$destroy", function() {
compiledElement.remove();
});
};
}]);
这确实看起来非常有希望,因为它修复了小提琴的例子。我会将它应用到我的应用程序中,看看它是否也能在那里工作 – kapoiosKapou
是的!这也适用于我更复杂的应用程序..! 虽然..我仍然不完全明白*为什么这个工程。我没有将它添加到DOM中,为什么它需要被删除? 另外,即使当我将编译的元素添加到DOM然后删除它,内存仍然泄漏。 $ compile是否在其他地方添加了这个?就像一个缓存..? – kapoiosKapou