Skip to content

Instantly share code, notes, and snippets.

@MikelArnaiz
Created November 25, 2016 09:59
Show Gist options
  • Save MikelArnaiz/c363192fc5bba69517ab7b5dd8aa711d to your computer and use it in GitHub Desktop.
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
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();
}
}
);
}
}
}
});
<!-- 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