Disable ui-sref based on a condition

前端 未结 7 617
没有蜡笔的小新
没有蜡笔的小新 2020-12-16 11:22

Can you tell me a way to disable the submit button, which changes to a new state by:

Submit

The butto

相关标签:
7条回答
  • 2020-12-16 11:31

    in ui-sref you can try and hide url based on codition for example i have this code and it works in urls

    <a ui-sref="category-edit({pk:category.id})" ng-show="canEdit(category.owner)" class="btn btn-primary"><i class="glyphicon glyphicon-pencil"></i></a>
    

    and in my controller

    $scope.canEdit = function(category){
       return category == AuthUser.username ;
    }
    

    Authuser is a factory in my main page

    <script>
        // Configure the current user
        var app = angular.module('myApp'); // Not including a list of dependent modules (2nd parameter to `module`) "re-opens" the module for 
        app.factory('AuthUser', function() {
          return {
           username: "{{ user.username|default:''|escapejs }}"
          }
       });
    </script>
    
    0 讨论(0)
  • 2020-12-16 11:33

    There is a pure CSS solution for the problem.

    Just set the pointer-events on your a tag to none:

    button[disabled] > a {
        pointer-events: none;
    }
    

    Of course you should set a more accurate CSS selector to target just the buttons you want.

    0 讨论(0)
  • 2020-12-16 11:34

    I had problems with the isolate scope and using other directives inside anchors, so here's my take, replacing the iscolate scope with regular attributes.

    angular.module('app').directive('uiSrefIf', ['$compile', function($compile) {
      var directive = {
        restrict: 'A',
        link: linker
      };
    
      return directive;
    
      function linker(scope, elem, attrs) {
        elem.removeAttr('ui-sref-if');
        $compile(elem)(scope);
    
        scope.$watch(attrs.condition, function(bool) {
          if (bool) {
            elem.attr('ui-sref', attrs.value);
          } else {
            elem.removeAttr('ui-sref');
            elem.removeAttr('href');
          }
    
          $compile(elem)(scope);
        });
      }
    }]);
    

    Html Looks like this.

    <a ui-sref-if condition="enableLink()" value="init.main">
        <cover class="card-image" card="card"></cover>
    </a>
    
    0 讨论(0)
  • 2020-12-16 11:41

    The easiest way that I found to do it conditionally in the template

    When your condition is met, i point it to the current state which doesn't activate the transition else I transition to the state needed.

    <a ui-sref="{{ 1==1 ? '.': 'state({param:here})'}}">Disabled Link</a> 
    
    0 讨论(0)
  • 2020-12-16 11:42

    My take on the directive provided by @m59 (without introducing an isolated scope):

    .directive('uiSrefIf', function($compile) {
        return {
            link: function($scope, $element, $attrs) {
    
                var uiSrefVal = $attrs.uiSrefVal,
                    uiSrefIf  = $attrs.uiSrefIf;
    
                $element.removeAttr('ui-sref-if');
                $element.removeAttr('ui-sref-val');
    
    
    
                $scope.$watch(
                    function(){
                        return $scope.$eval(uiSrefIf);
                    },
                    function(bool) {
                        if (bool) {
    
                            $element.attr('ui-sref', uiSrefVal);
                        } else {
    
                            $element.removeAttr('ui-sref');
                            $element.removeAttr('href');
                        }
                        $compile($element)($scope);
                    }
                );
            }
        };
    })
    
    0 讨论(0)
  • 2020-12-16 11:49

    You could simply pair it with ng-click so that ng-disabled will work.

    .controller('myCtrl', function($scope, $state) {
        // so that you can call `$state.go()` from your ng-click
        $scope.go = $state.go.bind($state);
    })
    
    <!-- call `go()` and pass the state you want to go to -->
    <button ng-disabled="!canSave()" ng-click="go('view')>Submit</button>
    

    Here's a more fancy way using a custom directive:

    angular.module('myApp', ['ui.router'])
    .config(function($stateProvider) {
      $stateProvider.state('home', {
        url: '/'
      });
    })
    .controller('myCtrl', function() {
      
    })
    .directive('uiSrefIf', function($compile) {
      return {
        scope: {
          val: '@uiSrefVal',
          if: '=uiSrefIf'
        },
        link: function($scope, $element, $attrs) {
          $element.removeAttr('ui-sref-if');
          $compile($element)($scope);
          
          $scope.$watch('if', function(bool) {
            if (bool) {
              $element.attr('ui-sref', $scope.val);
            } else {
              $element.removeAttr('ui-sref');
              $element.removeAttr('href');
            }
            $compile($element)($scope);
          });
        }
      };
    })
    ;
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.13/angular-ui-router.min.js"></script>
    
    <div ng-app="myApp" ng-controller="myCtrl">
      <form name="form">
        <input ng-model="foo" ng-required="true">
        <button ng-disabled="form.$invalid">
          <a ui-sref-if="!form.$invalid" ui-sref-val="home">test</a>
        </button>
      </form>
    </div>

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