1

I am using ui-router and am trying to detect when a controller belonging to a view that's being transitioned away from gets destroyed.

So I currently have a destroy listener like so:

$scope.$on('destroy', function(){
    mySpecialFunction();
});

However, when a state change occurs to a different view\controller, this destroy event never fires.

The state I am transitioning to, is a sibling state so I'm going from myParent.childA to myParent.childB (where childA has the destroy listener added).

If I was instead going from myParent.childA to myParent.childA.child1 then this would make sense since childA still exists in the hierarchy.

Can someone help me understand why the scope still exists in this scenario please?

mindparse
  • 6,115
  • 27
  • 90
  • 191
  • Possible duplicate, this [SO post](http://stackoverflow.com/questions/16094940/what-is-the-lifecycle-of-an-angularjs-controller) should help – BBauer42 Feb 29 '16 at 14:47
  • Ok thanks, I have taken a look, and I'm not sure there is anything there that will help me – mindparse Feb 29 '16 at 14:53

1 Answers1

2

The event name you want is "$destroy" not "destroy"

Try

$scope.$on('$destroy', function(){
    mySpecialFunction();
});

This should get triggered under the conditions mentioned in question

charlietfl
  • 170,828
  • 13
  • 121
  • 150