我正在创建一个幻灯片指令来显示一些选项。在标题中有3个按钮,即“后退”,“取消选择”和“应用”。现在当我点击任何一个按钮时,'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...')
});
}
};
}]);
答案 0 :(得分:0)
我已经成功解决了这个问题。回答我的问题,以便它可以帮助某人:
更新了指令代码:
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
};
}]);