javascript.memory-leaks icon indicating copy to clipboard operation
javascript.memory-leaks copied to clipboard

AngularJS directive memory leak

Open ufocoder opened this issue 8 years ago • 0 comments

Project need an example for AngularJS directive memory leak with URL to live example on JSFiddle (or something like that)

For example there's a memory leak

function(scope, element, attrs) {  
  element.on('click', function() {
    scope.selected = true
  })
}

And it could be fixed like the following:


function(scope, element, attrs) {  
  element.on('click', function() {
    scope.selected = true
  })
  scope.$on('$destroy', function() {
    element.off() // deregister all event handlers
  })
}

By the way for more information about memory leaks in AngularJS you could read here

ufocoder avatar Oct 14 '17 12:10 ufocoder