Last active
March 22, 2016 11:57
-
-
Save amlang/a4282acf19a2786f830b to your computer and use it in GitHub Desktop.
Conditional control structure for ui.router states
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
/** | |
* Conditional control structure for ui.router states | |
* | |
* usage in js: | |
* angular.module('myModuel',['ui.router','ui-sref-if') ...... | |
* | |
* usage in view: | |
* <a ui-sref=".nextstate" ui-sref-if="!myForm.$invalid">Next State</a> | |
* | |
* sass: | |
* a:hover{ | |
* cursor: pointer; | |
* &[disabled='disabled'], | |
* &.disabled{ | |
* cursor: not-allowed; | |
* } | |
* } | |
*/ | |
angular.module('uiSrefIf',[]) | |
.directive('uiSrefIf',function($compile){ | |
return { | |
scope: {if: '=uiSrefIf'}, | |
link: function($scope, $element, $attrs) { | |
$element.removeAttr('ui-sref-if'); | |
$compile($element)($scope); | |
$scope.uiSref = $attrs.uiSref; | |
$scope.$watch('if', function(bool) { | |
$element.toggleClass('disabled',!bool); | |
if (bool) { | |
$element.removeAttr('disabled','disabled'); | |
$element.attr('ui-sref', $scope.uiSref); | |
} | |
else { | |
$element.removeAttr('ui-sref') | |
$element.removeAttr('href') | |
$element.attr('disabled','disabled'); | |
} | |
$compile($element)($scope); | |
}); | |
} | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment