Last active
August 17, 2017 09:17
-
-
Save fgerschau/54c3d330f68ef727930ad883dd55b787 to your computer and use it in GitHub Desktop.
AngularJS Placeholder Directive w/ Bootstrap
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
// 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