ng-click still fires when div is (ng)disabled

后端 未结 5 1623
孤街浪徒
孤街浪徒 2021-02-03 20:15

Problem is that ng-click works on so event if cancelTicket === false it still fires ng-click. How can I stop that?

5条回答
  •  天涯浪人
    2021-02-03 20:29

    You can disable click events when an element with ng-click is disabled.

    jQuery:

    $('*[ng-click]').on('click',function(event) {
         var $el = $(event.target);
         if($el.attr('disabled')) {
            event.stopPropagation();
         }
    });
    

    Doing this on all DOM elements could produce unwanted results. Also, you will need to run the above on any new HTML updated on the DOM.

    Instead, we can modify just buttons to work as expected.

    Angular:

    angular.module('app').directive('button',function() {
         return {
              restrict: 'E',
              link: function(scope,el) {
                  var $el = angular.element(el);
                  $el.bind('click', function(event) {
                      if($el.attr('disabled')) {
                          event.stopImmediatePropagation();
                      }
                  });
              }
         }
    });
    

    I would not do the above on div elements as it would be to heavy. Instead, modify your approach so that button elements are only used for clickable interactions. You can then style them to look like other div elements.

提交回复
热议问题