【发布时间】:2019-01-21 09:48:45
【问题描述】:
我想使用以下服务在 angularjs 页面上显示自定义 html:
app.service('automaticFunctions', function ($timeout) {
this.init = function initAutomaticFunctions(scope, $elem, attrs) {
switch (scope.content.type) {
case 'list':
....
break;
case 'scroll':
....
break;
case 'custom':
function updateFrameContent() {
var frame = $($elem).find(".customFrame")[0];
if (scope.content.frameType === "url") {
scope.content.ngHide.frameContent = true;
scope.content.ngHide.frameUrl = false;
frame.src = scope.content.frameUrl;
}
if (scope.content.frameType === "html") {
scope.content.ngHide.frameUrl = true;
scope.content.ngHide.frameContent = false;
frame.onload = function () {
frame.contentWindow.document.write(scope.content.frameContent);
frame.onload = null;
};
frame.src = "";
// frame = null;
}
}
scope.$watchGroup(['content.frameType', 'content.frameContent', 'content.frameUrl'], function () {
updateFrameContent();
});
updateFrameContent();
break;
}
}
});
当我在页面之间路由时,我从指令中调用 init 过程 10 到 20 次。
我有大量内存泄漏,我不知道如何处理帧变量。如果我将其设置为null,则没有内存泄漏但不显示内容。
【问题讨论】:
-
您是否尝试过只设置一次
frame变量,然后像在initfnc 中那样重复使用它。您可以在initfnc 上方声明frame变量,并将var frame = $($elem).find(".customFrame")[0];行编辑为:if (!frame) frame = $($elem).find(".customFrame")[0]; -
不行,因为每次在init函数中,frame var都有不同的值
-
在分析应用程序时是否检查过是否有任何分离节点? developers.google.com/web/tools/chrome-devtools/memory-problems
-
是的,我有分离节点..
标签: javascript jquery angularjs memory-leaks