How to put a $ destroy listen when using controllerAs syntax in angular

Now I get rid of the dependency $scopeon my angular controller so that I can easily port my code to Angular2. My current version is angular 1.4.X. Performing the same thing, there is a place when I placed a $destroylistener over my area of ​​the controller, for example $scope.$on('$destory', function() ....).

I see a method $onavailable only on the $scopecontroller, but how can I achieve it without using a dependency $scope.

+4
source share
2 answers

angular 1.5+, , . $scope. $onDestroy() , , :

$onDestroy() - , . , .

http://blog.thoughtram.io/angularjs/2016/03/29/exploring-angular-1.5-lifecycle-hooks.html:

function MyCmpController($element) {

  var clickHandler = function () {
    // do something
  };

  this.$onInit = function () {
    $element.on('click', clickHandler);
  };

  this.$onDestroy = function () {
    $element.off('click', clickHandler);
  };
}
+4

, $destroy DOM, DOM $destroy, , DOM, .

, $element, , DOM, ng-controller. $element , ,

$element.on('$destroy', function(){
   //write clean up code here
});

1.4.X. 1.5.3+ Angular lifecycle hook, $onDestroy, @pgreen2 . :)

+3

All Articles