Last Updated: February 25, 2016
·
2.097K
· not-only-code

AngularJS Destroy $scope listeners

In AngularJS each listener on any $scope must be destroyed manually. To do that, when you creates a listener it returns a function to destroy it.

destroyCallback = $rootScope.$on 'my:event', ->
    console.log 'my code'

So you can destroy it when current $scope destroys

$scope.$on '$destroy', ->
    # unbind listener 'my:event'
    destroyCallback()

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

AngularJS pages are long-lived browser apps with the same script risk as a newer single-page app. Third-party code in the template can read forms and values rendered on the page.

Security insights from Cside