我有一个指令将一些函数绑定到本地范围$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');
});
}
};
});
但这并不理想。
是否可以一次将多个事件绑定到同一个函数?