AngularJS $ on - 重新订阅未订阅的广播

时间:2014-05-01 23:49:38

标签: javascript angularjs broadcast

要从$broadcast中的$on取消订阅,可以执行

var cleanUp = $rootScope.$on('myBroadcast', function(){ ... });
cleanUp();

调用cleanUp()后,有没有办法重新订阅?

1 个答案:

答案 0 :(得分:2)

function foo () {
  return $rootScope.$on('myBroadcast', function(){ ... });
}
var cleanup = foo(); // listen

// somewhere down the road
cleanup(); // unlisten

// somewhere down the road
foo(); // listen again