Skip to content

Instantly share code, notes, and snippets.

@vilmosioo
Forked from rewoo/my-awesome-directive.js
Last active August 29, 2015 14:05
Show Gist options
  • Save vilmosioo/5cfec8c0e289a6055a5e to your computer and use it in GitHub Desktop.
Save vilmosioo/5cfec8c0e289a6055a5e to your computer and use it in GitHub Desktop.
angular.module('app', [])
.directive('myAwsomeDirective', function($rootScope) {
return {
restrict: 'A',
link: function(scope) {
// $scope.$on() returns an unbind function of the binding
//
// So use the bind function directly within scope's $destroy event
// to unbind event automatically.
scope.$on('$destroy', $rootScope.$on('my:event', function() {
// your event code here
}));
}
}
});
angular.module('app', [])
.directive('myAwsomeDirective', function($rootScope) {
return {
restrict: 'A',
link: function(scope) {
// $scope.$on() returns an unbind function of the binding
var watch = $rootScope.$on('my:event', function() {
// your event code here
});
scope.$on('$destroy', watch);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment