我有一個指令將一些函數綁定到本地範圍$scope.$on
。
是否可以將同一個函數綁定到一個調用中的多個事件?
理想我可以做這樣的事情:
app.directive('multipleSadness', function() {
return {
restrict: 'C',
link: function(scope, elem, attrs) {
scope.$on('event:auth-loginRequired event:auth-loginSuccessful', function() {
console.log('The Ferrari is to a Mini what AngularJS is to ... other JavaScript frameworks');
});
}
};
});
但是,這是行不通的。以逗號分隔的事件名稱字符串替換爲['event:auth-loginRequired', 'event:auth-loginConfirmed']
的示例也不例外。
什麼工作是這樣的:
app.directive('multipleSadness', function() {
return {
restrict: 'C',
link: function(scope, elem, attrs) {
scope.$on('event:auth-loginRequired', function() {
console.log('The Ferrari is to a Mini what AngularJS is to ... other JavaScript frameworks');
});
scope.$on('event:auth-loginConfirmed', function() {
console.log('The Ferrari is to a Mini what AngularJS is to ... other JavaScript frameworks');
});
}
};
});
但這並不理想。
是否可以將多個事件綁定到同一個函數中?
感謝有關事件傳播和事件ID的額外信息,瞭解這一點,我明白爲什麼我所要求的不是本地可能的。事件確實有不同的監聽器,但是這個監聽器需要對多個事件執行相同的操作。 – 2013-02-12 20:02:00
經過考慮,我將我的代碼重構爲更多* Angular *。還有兩個事件,但由於它們表示不同的事物(在重構中),所以沒有理由將它們綁定到相同的函數。感謝指針。 – 2013-02-13 08:35:40
完全沒問題。很高興我能幫上忙。 – testing123 2013-02-13 16:29:21