Skip to content

Instantly share code, notes, and snippets.

@spidergears
Last active February 21, 2016 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spidergears/5b5e906872126751ef22 to your computer and use it in GitHub Desktop.
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 .
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