我已经使用$ on函数将我的监听器注册到$ broadcast事件
$scope.$on("onViewUpdated", this.callMe);
Run Code Online (Sandbox Code Playgroud)
我想根据特定的业务规则取消注册此侦听器.但我的问题是,一旦注册,我无法取消注册.
AngularJS中是否有任何方法可以取消注册特定的侦听器?像$ on这样取消注册此事件的方法可能是$ off.所以我可以说基于业务逻辑
$scope.$off("onViewUpdated", this.callMe);
Run Code Online (Sandbox Code Playgroud)
当有人播放"onViewUpdated"事件时,此函数停止被调用.
谢谢
编辑:我想从另一个函数取消注册监听器.不是我注册它的功能.
Liv*_* T. 471
您需要存储返回的函数并将其调用以取消订阅该事件.
var deregisterListener = $scope.$on("onViewUpdated", callMe);
deregisterListener (); // this will deregister that listener
Run Code Online (Sandbox Code Playgroud)
这可以在源代码:)中找到,至少在1.0.4中.我会发布完整的代码,因为它很短
/**
* @param {string} name Event name to listen on.
* @param {function(event)} listener Function to call when the event is emitted.
* @returns {function()} Returns a deregistration function for this listener.
*/
$on: function(name, listener) {
var namedListeners = this.$$listeners[name];
if (!namedListeners) {
this.$$listeners[name] = namedListeners = [];
}
namedListeners.push(listener);
return function() {
namedListeners[indexOf(namedListeners, listener)] = null;
};
},
Run Code Online (Sandbox Code Playgroud)
另外,请参阅文档.
小智 58
看看大多数回复,它们看起来过于复杂.Angular内置了取消注册的机制.
// Register and get a handle to the listener
var listener = $scope.$on('someMessage', function () {
$log.log("Message received");
});
// Unregister
$scope.$on('$destroy', function () {
$log.log("Unregistering listener");
listener();
});
Run Code Online (Sandbox Code Playgroud)
小智 27
这段代码适合我:
$rootScope.$$listeners.nameOfYourEvent=[];
Run Code Online (Sandbox Code Playgroud)
Ben*_*esh 10
编辑:正确的方法是@LiviuT的答案!
您总是可以扩展Angular的范围,以允许您删除这样的侦听器:
//A little hack to add an $off() method to $scopes.
(function () {
var injector = angular.injector(['ng']),
rootScope = injector.get('$rootScope');
rootScope.constructor.prototype.$off = function(eventName, fn) {
if(this.$$listeners) {
var eventArr = this.$$listeners[eventName];
if(eventArr) {
for(var i = 0; i < eventArr.length; i++) {
if(eventArr[i] === fn) {
eventArr.splice(i, 1);
}
}
}
}
}
}());
Run Code Online (Sandbox Code Playgroud)
以下是它的工作原理:
function myEvent() {
alert('test');
}
$scope.$on('test', myEvent);
$scope.$broadcast('test');
$scope.$off('test', myEvent);
$scope.$broadcast('test');
Run Code Online (Sandbox Code Playgroud)
在调试代码之后,我创建了自己的函数,就像"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);
}
}
}
}
});
Run Code Online (Sandbox Code Playgroud)
所以通过将我的函数附加到$ rootscope现在它可供我的所有控制器使用.
在我的代码中,我正在做
$scope.$off("onViewUpdated", callMe);
Run Code Online (Sandbox Code Playgroud)
谢谢
编辑:AngularJS的方法是@LiviuT的回答!但是,如果要在另一个范围内取消注册侦听器,同时又希望远离创建局部变量以保留de-registeration函数的引用.这是一种可能的解决方案.
| 归档时间: |
|
| 查看次数: |
113636 次 |
| 最近记录: |