【发布时间】:2015-06-08 03:51:46
【问题描述】:
我有一个指令,它使用$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');
});
}
};
});
但这并不理想。
是否可以一次性将多个事件绑定到同一个函数?
【问题讨论】:
标签: angularjs angularjs-directive