Skip to content Skip to sidebar Skip to footer

Angularjs - $scope.$destroy Doesn't Remove Watchers

I am trying to figure out how to create my own 'one time binding', for Angularjs <= 1.2. I found this answer, describing how to create your own bindOnce directive. I do see that

Solution 1:

Use the cleanup function to remove watchers:

function cleanup(element) 
  { 
  element.off().removeData();

  var injector = currentSpec.$injector;

  element.$injector = null;

  // clean up jquery's fragment cache
  angular.forEach(angular.element.fragments, function(val, key) {
    delete angular.element.fragments[key];
  });

  angular.forEach(angular.callbacks, function(val, key) 
    {
    delete angular.callbacks[key];
    });

  angular.callbacks.counter = 0;
  }

Use a self-destructing service as a simple bind once:

function onetime()
  {
  /*...*/
  onetime = Function;
  }

angular.module('myApp').constant('bindonce', onetime);

angular.module('myApp').controller('foo', function($bindonce){
  this.$inject = '$bindonce';
  $scope.mybind = $bindonce();
} 

Use the migration guide for reference to find breaking changes:

References


Post a Comment for "Angularjs - $scope.$destroy Doesn't Remove Watchers"