调试代码后,我创建了自己的函数,就像“ blesh”的答案一样。这就是我所做的
MyModule = angular.module('FIT', [])
.run(function ($rootScope) {
// Custom $off function to un-register the listener.
$rootScope.$off = function (name, listener) {
var namedListeners = this.$$listeners[name];
if (namedListeners) {
// Loop through the array of named listeners and remove them from the array.
for (var i = 0; i < namedListeners.length; i++) {
if (namedListeners[i] === listener) {
return namedListeners.splice(i, 1);
}
}
}
}
});
因此,通过将我的函数附加到$ rootscope,它现在可用于我的所有控制器。
在我的代码中
$scope.$off("onViewUpdated", callMe);
谢谢
编辑:AngularJS的方法是在@LiviuT的答案!但是,如果要在另一个作用域中注销侦听器,同时又要避免创建局部变量以保留注销功能的引用。这是一个可能的解决方案。