如何销毁angularjs应用程序? [英] How to destroy an angularjs app?

查看:34
本文介绍了如何销毁angularjs应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够动态加载/卸载 Angular 应用程序而不会导致内存泄漏.在 jQuery 中,您可以执行 $("#elementHoldingMyWidget").remove(); 并执行适当的销毁代码,事件处理程序未绑定等.

I need to be able to load/unload angular applications dynamically without causing memory leaks. In jQuery you can do $("#elementHoldingMyWidget").remove(); and the proper destruction code gets executed, event handlers are unbound etc.

我一直无法在 angular 文档中找到任何提到一旦启动应用程序就有可能将其拆除的内容.

I've been unable to find anything in the angular docs mentioning the possibility of tearing down an app once it's been bootstrapped.

我的第一次尝试是像这样销毁 rootScope:

My 1st attempt was to destroy the rootScope like so:

var rootScope = $("body").scope();   
rootScope.$destroy();

但这似乎不起作用,我不确定即使这样做了注入器和服务也会如何清理.

But this doesn't seem to be working, and I'm not sure how the injector and services would be cleaned up even if it did.

这应该怎么做?

推荐答案

UPDATE March 10, 2013: 我发现 $('body').empty();不会拆除应用程序.它还活着.

UPDATE March 10, 2013: I found that $('body').empty(); does not tear down the app. It still lives.

原帖:

好吧,这篇文章:https://github.com/angular/angular.js/issues/1537#issuecomment-10164971 声称没有官方"应用程序被拆除(在撰写本文时),但您可以像这样清空包含应用程序的元素:>

Well, this post: https://github.com/angular/angular.js/issues/1537#issuecomment-10164971 claims that there is no 'official' app tear down (at the time of writing), but you can just empty the element holding the app like so:

$('body').empty();

如果这不是您要找的内容,您可以通过以下步骤找到一个临时解决方案来拆除您的应用:https://github.com/angular/angular.js/issues/1537#issuecomment-10184033

If this is not what you are looking for you can go through these steps for a temporary solution to tearing your app down: https://github.com/angular/angular.js/issues/1537#issuecomment-10184033

这篇关于如何销毁angularjs应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆