Javascript AngularJS指令多选择器
我想使我的指令成为多个选择器 我的指令是提醒每一个水龙头 我已经编写了以下代码:Javascript AngularJS指令多选择器,javascript,angularjs,directive,Javascript,Angularjs,Directive,我想使我的指令成为多个选择器 我的指令是提醒每一个水龙头 我已经编写了以下代码: angular .module('app.directives') .directive('onTap', someDirective) .directive('button', someDirective); someDirective.$inject = ['$ionicGesture']; function someDirective($ionicGesture) { re
angular
.module('app.directives')
.directive('onTap', someDirective)
.directive('button', someDirective);
someDirective.$inject = ['$ionicGesture'];
function someDirective($ionicGesture) {
return {
restrict: 'EA',
link: link
};
function link($scope, $elem, $attrs) {
$ionicGesture.on('tap', function() {
alert('Tapped!');
}, $elem);
}
}
此代码问题与此按钮有关,例如:
<button on-tap="doSomething()">Do something</button>
但这意味着我必须将它添加到每个按钮
或[onTap]
选择器
有什么好的解决方案吗?您可以尝试以下方法:
function someDirective($ionicGesture) {
var directive = {
restrict: 'EA',
link: link
};
return directive;
function link($scope, $elem, $attrs, ctrls) {
if (directive.name === 'onTap' && $elem.is('button')) {
return;
}
$ionicGesture.on('tap', function() {
alert('Tapped!');
}, $elem);
}
}
我不是这个意思。我的意思是,我想制作一个选择器列表,这些选择器将在点击时执行警报功能。如果元素在我的列表中多于一个选择器(按钮[onTap]),那么警报将只执行一次而不是两次。
function someDirective($ionicGesture) {
var directive = {
restrict: 'EA',
link: link
};
return directive;
function link($scope, $elem, $attrs, ctrls) {
if (directive.name === 'onTap' && $elem.is('button')) {
return;
}
$ionicGesture.on('tap', function() {
alert('Tapped!');
}, $elem);
}
}