LoginSignup
1
1

More than 5 years have passed since last update.

$rootScope.$onで登録した処理をキャンセルする方法

Last updated at Posted at 2016-05-30

シングルトンなサービスとかで使うとハマりがちなやつ

var hogeCanceller;
service.bindHoge = function(cb){

  // hoge処理が登録済みの場合はキャンセルする
  if( hogeCanceller ) hogeCanceller();

  // $onで登録した処理をキャンセルする処理を取得
  hogeCanceller = $rootScope.$on(
    'hoge',
    function(){
      //...
      cb();
    }
   );
};

ちなみに以下のようにして、登録済み処理のキャンセル処理を取得することはできない

hogeCanceller = $rootScope.$on('hoge');
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1