Angular 指令触发事件两次
Angular directive triggering the event twice
我正在创建一个 slide-in 指令来显示一些选项。标题中有3个按钮,分别是'Back'、'deselectall'和'Apply'。现在,当我单击任何按钮时, 'click' 事件会被触发两次。请看下面的代码。
HTML:
<aside class="search-filter-slide fullheight search-filter ng-hide">
<div class="search-filter-by clearfix row" id="searchFiltersAction">
<button class="btn btn-sm btn-info pull-left" id="searchFilterBackBtn">Back</button>
<button class="btn btn-sm btn-link" id="deselectAllFilters">deselect all</button>
<button class="btn btn-sm btn-primary pull-right" id="applyFiltersBtn">Apply</button>
</div>
</aside>
指令:
angular.module('MyAppObj')
.directive('searchFilter', [function () {
return {
restrict: 'CA',
link: function (scope, element, attrs) {
$(document).on('click','#searchFilterBtn.search-filter-btn',function(e){
e.stopPropagation();
$(element).removeClass('ng-hide');
setTimeout(function () { $(element).addClass('slide-in'); }, 200);
});
$(document).on('click','#searchFiltersAction #searchFilterBackBtn',function(e){
e.stopPropagation();
console.log('Closing the open filters view...',e.target.tagName)
$(element).removeClass('slide-in');
setTimeout(function () { $(element).addClass('ng-hide'); }, 500);
});
$(document).on('click','#searchFiltersAction #deselectAllFilters',function(e){
e.stopPropagation();
console.log('Deselect all selected filters...')
});
$(document).on('click','#searchFiltersAction #applyFiltersBtn',function(e){
e.stopPropagation();
console.log('Apply selected filters to the search results...')
});
}
};
}]);
我已经成功解决了这个问题。回答我的问题,以便它可以帮助别人:
更新指令代码:
angular.module('MyAppObj')
.directive('searchFilter', [function () {
function link(scope, element, attrs){
$(document).on('click','#searchFilterBtn.search-filter-btn',function(e){
e.stopPropagation();
$(element).removeClass('ng-hide');
setTimeout(function () { $(element).addClass('slide-in'); }, 200);
});
$(document).on('click','#searchFiltersAction #searchFilterBackBtn',function(e){
e.stopPropagation();
console.log('Closing the open filters view...',e.target.tagName)
$(element).removeClass('slide-in');
setTimeout(function () { $(element).addClass('ng-hide'); }, 500);
});
$(document).on('click','#searchFiltersAction #deselectAllFilters',function(e){
e.stopPropagation();
console.log('Deselect all selected filters...')
});
$(document).on('click','#searchFiltersAction #applyFiltersBtn',function(e){
e.stopPropagation();
console.log('Apply selected filters to the search results...')
});
var cleanup = function(){
$(document).off('click','#searchFilterBtn.search-filter-btn');
$(document).off('click','#searchFiltersAction #searchFilterBackBtn');
$(document).off('click','#searchFiltersAction #deselectAllFilters');
$(document).off('click','#searchFiltersAction #applyFiltersBtn');
}
scope.$on('$destroy',cleanup)
}
return {
restrict: 'CA',
link: link
};
}]);
我正在创建一个 slide-in 指令来显示一些选项。标题中有3个按钮,分别是'Back'、'deselectall'和'Apply'。现在,当我单击任何按钮时, 'click' 事件会被触发两次。请看下面的代码。
HTML:
<aside class="search-filter-slide fullheight search-filter ng-hide">
<div class="search-filter-by clearfix row" id="searchFiltersAction">
<button class="btn btn-sm btn-info pull-left" id="searchFilterBackBtn">Back</button>
<button class="btn btn-sm btn-link" id="deselectAllFilters">deselect all</button>
<button class="btn btn-sm btn-primary pull-right" id="applyFiltersBtn">Apply</button>
</div>
</aside>
指令:
angular.module('MyAppObj')
.directive('searchFilter', [function () {
return {
restrict: 'CA',
link: function (scope, element, attrs) {
$(document).on('click','#searchFilterBtn.search-filter-btn',function(e){
e.stopPropagation();
$(element).removeClass('ng-hide');
setTimeout(function () { $(element).addClass('slide-in'); }, 200);
});
$(document).on('click','#searchFiltersAction #searchFilterBackBtn',function(e){
e.stopPropagation();
console.log('Closing the open filters view...',e.target.tagName)
$(element).removeClass('slide-in');
setTimeout(function () { $(element).addClass('ng-hide'); }, 500);
});
$(document).on('click','#searchFiltersAction #deselectAllFilters',function(e){
e.stopPropagation();
console.log('Deselect all selected filters...')
});
$(document).on('click','#searchFiltersAction #applyFiltersBtn',function(e){
e.stopPropagation();
console.log('Apply selected filters to the search results...')
});
}
};
}]);
我已经成功解决了这个问题。回答我的问题,以便它可以帮助别人:
更新指令代码:
angular.module('MyAppObj')
.directive('searchFilter', [function () {
function link(scope, element, attrs){
$(document).on('click','#searchFilterBtn.search-filter-btn',function(e){
e.stopPropagation();
$(element).removeClass('ng-hide');
setTimeout(function () { $(element).addClass('slide-in'); }, 200);
});
$(document).on('click','#searchFiltersAction #searchFilterBackBtn',function(e){
e.stopPropagation();
console.log('Closing the open filters view...',e.target.tagName)
$(element).removeClass('slide-in');
setTimeout(function () { $(element).addClass('ng-hide'); }, 500);
});
$(document).on('click','#searchFiltersAction #deselectAllFilters',function(e){
e.stopPropagation();
console.log('Deselect all selected filters...')
});
$(document).on('click','#searchFiltersAction #applyFiltersBtn',function(e){
e.stopPropagation();
console.log('Apply selected filters to the search results...')
});
var cleanup = function(){
$(document).off('click','#searchFilterBtn.search-filter-btn');
$(document).off('click','#searchFiltersAction #searchFilterBackBtn');
$(document).off('click','#searchFiltersAction #deselectAllFilters');
$(document).off('click','#searchFiltersAction #applyFiltersBtn');
}
scope.$on('$destroy',cleanup)
}
return {
restrict: 'CA',
link: link
};
}]);