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

后端 未结 5 1555
孤街浪徒
孤街浪徒 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:26

    You should change DIV tag to Button Tag. It works for me.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • Event is triggered even if the div is disabled.

    You can avoid this by using lazy evaluation of expressions like isDisabled || action() so action would not be called if isDisabled is true.

    In your case it will be:

    ng-click="cancelTicket === false || CancelTicket(ticketPin)"
    
    0 讨论(0)
  • 2021-02-03 20:43
    <a class="btn btn-danger btn-xs" ng-click="vm.handleRemove(device)" ng-disabled="status === 1">Delete</a>
    

    Change a tag to button tag then OK

    <button class="btn btn-danger btn-xs" ng-click="vm.handleRemove(device)" ng-disabled="status === 1">Delete</button>
    
    0 讨论(0)
  • 2021-02-03 20:47

    My solution has been to use an html directive with an attribute used instead of ng-click, so that

    <html-tag-directive new-click="ctrl.functionToCall()" disabled="ctrl.disabled" >
    

    and the directive defined as follow:

    1) template:

    <button type="button" 
       ng-disabled="disabled" ng-click="onClick()">
    </button>
    

    2) controller:

    angular.module('module')
    .directive('htmlTagDirective',function() {
        return {
            restrict:'E',
            templateUrl:'template.html',
            scope:{
                disabled:'=',
                click: '&'
            },
            link:function(scope,element){
    
                scope.onClick = function() {
                    if (!(scope.disabled)) {
                        scope.newClick();
                    }
                };
    
            }
        };
    });
    
    0 讨论(0)
提交回复
热议问题