Disable ui-sref based on a condition

Problem

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

<a ui-sref="state">Submit</a>

The button should be enabled only when the form is valid.

ng-disabled with ui-sref does not work:

<form name="tickets">
  <button ng-disabled="!canSave()"><a ui-sref="view">Submit</a></button>
</form>

canSave function inside app.js being:

$scope.canSave = function(){
  return $scope.tickets.$dirty && $scope.tickets.$valid;
};
Problem courtesy of: Aakash

Solution

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>

Solution courtesy of: m59

Discussion

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> 
Discussion courtesy of: jetpackdata.com

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>
Discussion courtesy of: Dimitris Kougioumtzis

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>
Discussion courtesy of: user3257696

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);
                }
            );
        }
    };
})
Discussion courtesy of: klmdb

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.

Discussion courtesy of: Michael Günter

This recipe can be found in it's original form on Stack Over Flow.