AngularJS控制器析构函数 [英] Angularjs Controller destructor

查看:123
本文介绍了AngularJS控制器析构函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个AngularJs应用.我将控制器用于某些子范围.在每个控制器中,我可以设置许多属于相应子作用域的变量.当AngularJs实例化一个控制器时,有一个构造函数,我可以在其中为子作用域变量设置默认值.

I have an AngularJs app. I use Controllers for some child scopes. In every Controller I can set a number of variables that belong to the corresponding Child Scope. When AngularJs instantiate a controller, there is a constructor where I can set a default value to my child-scope variables.

我有一个控制器析构函数"吗?我怎么知道何时关闭控制器并且正在清理作用域(由$ destroy函数破坏)?

Do I have a controller "destructor"? How do I know when a controller is closing and the scope is being cleaned (destroyed by the $destroy function)?

谢谢!

推荐答案

您必须收听$destroy 事件,例如:

function MyController($scope, ...) {
    ...
    $scope.$on("$destroy", function handler() {
        // destruction code here
    });
}

相关文档: https://docs.angularjs.org/api/ng/type/$ rootScope.Scope

这篇关于AngularJS控制器析构函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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