$ onDestroy&之间的Angular 1.5组件差异$ scope.$ destroy() [英] Angular 1.5 Components difference between $onDestroy & $scope.$destroy()

查看:127
本文介绍了$ onDestroy&之间的Angular 1.5组件差异$ scope.$ destroy()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解控制器的 $ onDestroy 方法和 $ scope.$ destroy()之间的区别.

I'm trying to understand the difference between the controller's $onDestroy method and $scope.$destroy().

定义说,当需要破坏组件的包含范围时,将调用$ onDestroy.但是$ scope.$ destroy()的情况不一样吗?

Definition says that $onDestroy is called when the containing scope of the component needs to be destroyed. But isn't it the same case with $scope.$destroy()?

根据我在 https://plnkr.co/edit/9RlS8OLxAoyK80WPMJaN?p =预览

<div ng-controller="ParentController">
    <button ng-click="func()">Parent</button>
    <br><br>
    <div ng-controller="ChildController">
      <button ng-click="childFunc()"> Child </button>
      <br><br>
      <comp> </comp>
    </div>
</div>

我注意到了两种情况.

  1. 当我从Parent或Child控制器调用$ destroy()时,在组件中会同时触发$ onDestroy和$ scope.$ on('$ destroy')事件.这是有道理的,因为在销毁父范围时会销毁组件的包含范围.但是,我仍然能够单击"Component FUNC"按钮并调用与之关联的函数.为什么会这样?

  1. When I call $destroy() from the Parent or Child controller, both $onDestroy and $scope.$on('$destroy') events are triggered in the Component. This makes sense as the containing scope of the component is destroyed when the Parent scope is destroyed. However, I'm still able to click the 'Component FUNC' button and call the function associated with it. Why does this happen?

当我从Component本身调用$ destroy()时,仅触发$ scope.$ on('destroy')事件,而不会触发$ onDestroy行为.另外,我无法像案例1那样访问"Component FUNC"按钮.

When I call $destroy() from the Component itself, then only the $scope.$on('destroy') event is triggered leaving behing the $onDestroy untriggered. Also, I'm not able to access the 'Component FUNC' button as I was able to do in Case #1.

有人可以解释一下这两种情况的区别吗?

Can someone please explain me the difference in the two cases?

这不是现实生活中的用例,而是我想了解的东西.

This is not a real life use-case but something that I wanted to understand.

谢谢.

推荐答案

看看以下链接:

https://github.com/angular/angular.js/issues/15073
https://github.com/angular/angular.js/issues/14376

Take a look at the following links:

https://github.com/angular/angular.js/issues/15073
https://github.com/angular/angular.js/issues/14376

总而言之,他们给出的解释如下:

To summarize, the explanation they give is the following:

有一个误解,认为controller.$ onDestroy()等同于$ scope.$ on('$ destroy'),其中$ scope是传递给控制器​​的作用域.并非总是如此.

There is a misconception that controller.$onDestroy() is equivalent to $scope.$on('$destroy'), where $scope is the scope passed to the controller. This is not always the case.

更具体地,当控制器存在于"范围内时,调用controller.$ onDestroy().被摧毁.当您具有隔离作用域指令时,传递给控制器​​的作用域(作为$ scope)不是控制器所在的作用域,而是为该指令的模板创建的子作用域.因此,调用$ scope.$ destroy()不会破坏控制器所在的作用域(也不调用controller.$ onDestroy()).

More specifically, controller.$onDestroy() is called when the scope where the controller "lives in" gets destroyed. When you have an isolate scope directive, the scope passed (as $scope) to the controller is not the scope that the controller lives in, it is a child-scope that is created for the directive's template. Thus, calling $scope.$destroy() will NOT destroy the scope that the controller lives in (and NOT call controller.$onDestroy()).

唯一的情况是,当controller.$ onDestroy()等效于$ scope.$ on('$ destroy')时,指令范围为false.这意味着不会创建新的作用域,因此传递给控制器​​的作用域(作为$ scope)与控制器所在的作用域相同.

The only case, where controller.$onDestroy() is equivalent to $scope.$on('$destroy') is when having a directive with scope: false. This means that no new scope is created, so the scope passed (as $scope) to the controller is the same scope that the controller lives in.

这解释了第二种情况,但是我仍然不明白为什么即使在调用$ destroy()之后也可以调用与"Component FUNC"按钮关联的函数.

This explains the second situation, but I still don't get why you can call the function associated with the 'Component FUNC' button even after you have called $destroy().

我希望这会有所帮助.

这篇关于$ onDestroy&amp;之间的Angular 1.5组件差异$ scope.$ destroy()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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