Skip to content

Instantly share code, notes, and snippets.

@fgerschau
Last active August 17, 2017 09:17
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 fgerschau/54c3d330f68ef727930ad883dd55b787 to your computer and use it in GitHub Desktop.
Save fgerschau/54c3d330f68ef727930ad883dd55b787 to your computer and use it in GitHub Desktop.
AngularJS Placeholder Directive w/ Bootstrap
// Usage in pug/jade:
// placeholder(loadingdata="posts" height="300px")
// -> waits for 'posts' to be loaded
angularApp.directive('placeholder', function ($parse) {
function compile(element, attrs) {
element.children().attr('ng-show', attrs.loadingdata);
element.hide();
var height = attrs.height;
var placeholderHtml = "<div class='jumbotron' id='jumbotron-placeholder' style='width: 100%; height:" + height + "' ></div>'";
var placeholder = $(placeholderHtml);
placeholder.insertAfter(element);
return function (scope, element, attrs) {
scope.$watch('loadingdata', function () {
if (scope.loadingdata && scope.loadingdata.length) {
placeholder.remove();
element.show();
} else {
element.hide();
placeholder.insertAfter(element);
}
}, true);
};
}
return {
restrict: 'E',
replace: true,
transclude: false,
scope: {
loadingdata: '=',
},
compile: compile,
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment