javascript - Angular 1.5 Components difference between $onDestroy & $scope.$destroy() - Stack Overflow

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

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

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

As per the plunker I've created at ,

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

I've noticed two situations.

  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 ponent 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?

  2. 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.

Thank you.

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

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

As per the plunker I've created at https://plnkr.co/edit/9RlS8OLxAoyK80WPMJaN?p=preview ,

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

I've noticed two situations.

  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 ponent 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?

  2. 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.

Thank you.

Share Improve this question asked Jan 22, 2017 at 8:11 Yash KapilaYash Kapila 2711 gold badge7 silver badges17 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

Take a look at the following links:

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

To summarize, the explanation they give is the following:

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.

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()).

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.

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().

I hope this helps.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745477095a4629394.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信