角度指令触发事件两次



我正在创建一个滑入指令来显示一些选项。标题中有3个按钮,即"返回"、"取消全选"one_answers"应用"。现在,当我点击任何一个按钮时,"点击"事件会触发两次。请查看以下代码。

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
  };
 }]);

相关内容

  • 没有找到相关文章

最新更新