いくつかの関数をローカルスコープにバインドするディレクティブがあり$scope.$on
ます。
1回の呼び出しで同じ関数を複数のイベントにバインドすることは可能ですか?
理想的には、次のようなことができるでしょう。
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');
});
}
};
});
しかし、これは理想的ではありません。
複数のイベントを一度に同じ関数にバインドすることは可能ですか?