Created
November 25, 2016 09:59
-
-
Save MikelArnaiz/c363192fc5bba69517ab7b5dd8aa711d to your computer and use it in GitHub Desktop.
A better ng-cloak angularjs directive. Hide elements until ng-hide and ng-show finish
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
angular.module('myApp') | |
.directive('betterCloak', function() { | |
return { | |
scope: { | |
'ngHide': '=', | |
'ngShow': '=' | |
}, | |
link: function(scope,element, attrs){ | |
if (typeof scope.ngHide == 'undefined' && typeof scope.ngShow == 'undefined' || scope.ngHide === false || scope.ngShow === true) { | |
$(element).addClass('better-cloak-ready'); | |
} else { | |
var deregisterWatch = scope.$watch( | |
function(){ | |
return element.attr('class').match(/ng-hide/g) && !element.attr('class').match(/ng-hide-/g); | |
}, | |
function(isReady){ | |
if(isReady){ | |
$(element).addClass('better-cloak-ready'); | |
deregisterWatch(); | |
} | |
} | |
); | |
} | |
} | |
} | |
}); |
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
<!-- Styles to add to head tag before other styles --> | |
<style> | |
[better-cloak]:not(.better-cloak-ready){ | |
display: none !important; | |
} | |
</style> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment