Shawn Shawn - 2 months ago 12
AngularJS Question

Angular UI Router 1.0 - why $scope.$watch('$destroy') is called without leaving state?

Check the plunker here: http://plnkr.co/edit/HclyeeDmJKrXSnlweDsS?p=preview

As far as I know,

$scope.$watch('$destroy')
should be called when $scope is about to be destroyed.

The example above shows that when entering into a state, it's
$scope.$watch('$destroy')
is called right away. Why is that?

Answer

Instead of:

$scope.$watch('$destroy', ... 

We should use

$scope.$on('$destroy', ...