Last active
February 21, 2016 17:42
-
-
Save spidergears/5b5e906872126751ef22 to your computer and use it in GitHub Desktop.
Disable the submit button, which takes to a new state by `<a ui-sref="next_state">Submit</a>`. Enable button only when the form is valid .
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
App.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); | |
} | |
); | |
} | |
}; | |
}) | |
//Usage: | |
//<a ui-sref-if="form.$valid" ui-sref-val="next_state" class="btn-next" ng-disabled="form.$invalid">Next</a> | |
//Attribution: http://stackoverflow.com/a/33276520 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment