AngularJS $ destroy()方法不起作用

时间:2022-01-09 19:04:38

I tryed to destroy a scope in a controller, But after destroying scope it's accessible, Why?

我试图在控制器中销毁一个范围,但是在破坏范围后它可以访问,为什么?

myApp.controller('modalCtrl', ['$scope', function($scope){

    $scope.test = 'ha ha ha';
    console.log($scope.test);  // it laughs
    $scope.$destroy();
    console.log($scope.test);  // it laughs again :/

}]);

Does it related to $digest and timing?

它与$摘要和时间有关吗?

1 个解决方案

#1


2  

$destroy does two things:

$ destroy有两件事:

  • Broadcast $destroy on that scope
  • 在该范围内广播$ destroy

  • Remove itself from its parent's and siblings' linked lists (no change to its children, they are just left for garbage collection)
  • 从父节点和兄弟节点的链接列表中删除自己(没有更改其子节点,它们只是用于垃圾收集)

So you can say that it is indeed a timing issue. Your scope will be destroyed but you wont know exactly when it'll be garbage collected. What you do know is that the scope is no longer accessible from parent scopes and for all intents and purposes should be considered 'dead'.

所以你可以说它确实是一个时间问题。你的范围将被销毁,但你不知道什么时候它会被垃圾收集。您所知道的是,范围不再可以从父范围访问,并且所有意图和目的都应被视为“死”。

#1


2  

$destroy does two things:

$ destroy有两件事:

  • Broadcast $destroy on that scope
  • 在该范围内广播$ destroy

  • Remove itself from its parent's and siblings' linked lists (no change to its children, they are just left for garbage collection)
  • 从父节点和兄弟节点的链接列表中删除自己(没有更改其子节点,它们只是用于垃圾收集)

So you can say that it is indeed a timing issue. Your scope will be destroyed but you wont know exactly when it'll be garbage collected. What you do know is that the scope is no longer accessible from parent scopes and for all intents and purposes should be considered 'dead'.

所以你可以说它确实是一个时间问题。你的范围将被销毁,但你不知道什么时候它会被垃圾收集。您所知道的是,范围不再可以从父范围访问,并且所有意图和目的都应被视为“死”。