Skip to content

Instantly share code, notes, and snippets.

@khoand0000
Created August 14, 2015 20:57
Show Gist options
  • Save khoand0000/93563b303124bf2c685d to your computer and use it in GitHub Desktop.
Save khoand0000/93563b303124bf2c685d to your computer and use it in GitHub Desktop.
Notice when using $on and $emit
function register(id, scope, success, error) 
{
  var handler = $rootScope.$on('settings:get' + id + scope.$id, success);
  scope.$on('$destroy', handler);
  // some code
  $rootScope.$emit('settings:get' + id + scope.$id, settings);
}
  • side effect if missing + id + scope.$id is that 2 controllers call register(id, $scope, success, error), when $emit occurs, it notify 2 controllers
  • use unique text by combine scope.$id + some information (like id or keyword, ...) avoid side effect.
  • the function can't use by factories because they don't have $scope
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment