Enable/Disable Anchor Tags using AngularJS

后端 未结 11 1447
一整个雨季
一整个雨季 2020-12-01 05:45

How do I enable/disable anchor tags using the directive approach?

Example:

  1. while clicking on edit link, create & delete needs to be disabled or g
相关标签:
11条回答
  • 2020-12-01 06:39

    You may, redefine the a tag using angular directive:

    angular.module('myApp').directive('a', function() {
      return {
        restrict: 'E',
        link: function(scope, elem, attrs) {
          if ('disabled' in attrs) {
            elem.on('click', function(e) {
              e.preventDefault(); // prevent link click
            });
          }
        }
      };
    });
    

    In html:

    <a href="nextPage" disabled>Next</a>
    
    0 讨论(0)
  • 2020-12-01 06:40

    My problem was slightly different: I have anchor tags that define an href, and I want to use ng-disabled to prevent the link from going anywhere when clicked. The solution is to un-set the href when the link is disabled, like this:

    <a ng-href="{{isDisabled ? '' : '#/foo'}}"
       ng-disabled="isDisabled">Foo</a>
    

    In this case, ng-disabled is only used for styling the element.

    If you want to avoid using unofficial attributes, you'll need to style it yourself:

    <style>
    a.disabled {
        color: #888;
    }
    </style>
    <a ng-href="{{isDisabled ? '' : '#/foo'}}"
       ng-class="{disabled: isDisabled}">Foo</a>
    
    0 讨论(0)
  • 2020-12-01 06:41

    ui-router v1.0.18 introduces support for ng-disabled on anchor tags

    Example: <a ui-sref="go" ng-disabled="true">nogo</a>

    • https://github.com/angular-ui/ui-router/issues/2957

    • https://github.com/angular-ui/ui-router/pull/3692/commits/a59fcae300f9d8f73a5b91fa77c92b926e68281d

    0 讨论(0)
  • 2020-12-01 06:47

    Modifying @Nitin's answer to work with dynamic disabling:

    angular.module('myApp').directive('a', function() {
      return {
        restrict: 'E',
        link: function(scope, elem, attrs) {
          elem.on('click', function(e) {
            if (attrs.disabled) {
              e.preventDefault(); // prevent link click
            }
          });
        }
      };
    });
    

    This checks the existence of disabled attribute and its value upon every click.

    0 讨论(0)
  • 2020-12-01 06:51

    Have you tried using lazy evaluation of expressions like disabled || someAction()?

    Lets assume I defined something like so in my controller:

    $scope.disabled = true;
    

    Then I can disabling a link and apply inline styles like so:

    <a data-ng-click="disabled || (GoTo('#/employer/'))" data-ng-style="disabled && { 'background-color': 'rgba(99, 99, 99, 0.5)', }">Higher Level</a>
    

    Or better still disable a link and apply a class like so:

    <a data-ng-click="disabled || (GoTo('#/employer/'))" data-ng-class="{ disabled: disabled }">Higher Level</a>
    

    Note: that you will have a class="disabled" applied to DOM element by that statement.

    At this stage you just need to handle what you action GoTo() will do. In my case its as simple as redirect to associated state:

    $scope.GoTo = function (state) {
        if (state != undefined && state.length > 0) {
            $window.location.hash = state;
        }
    };
    

    Rather than being limited by ngDisabled you are limited by what you decide to do.

    With this technique I successfully applied permission level checking to enable or disable user access to certain part of my module.

    Simple plunker to demonstrate the point

    0 讨论(0)
提交回复
热议问题