Last active
August 29, 2015 14:14
-
-
Save gsans/01af9bbab618a99747ac to your computer and use it in GitHub Desktop.
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
.factory("injectCSS", ['$q', '$http', 'MeasurementsService', function($q, $http, MeasurementsService){ | |
var injectCSS = {}; | |
var createLink = function(id, url) { | |
var link = document.createElement('link'); | |
link.id = id; | |
link.rel = "stylesheet"; | |
link.type = "text/css"; | |
link.href = url; | |
return link; | |
} | |
var checkLoaded = function (url, deferred, tries) { | |
for (var i in document.styleSheets) { | |
var href = document.styleSheets[i].href || ""; | |
if (href.split("/").slice(-1).join() === url) { | |
deferred.resolve(); | |
return; | |
} | |
} | |
tries++; | |
setTimeout(function(){checkLoaded(url, deferred, tries);}, 50); | |
}; | |
injectCSS.set = function(id, url){ | |
var tries = 0, | |
deferred = $q.defer(), | |
link; | |
if(!angular.element('link#' + id).length) { | |
link = createLink(id, url); | |
link.onload = deferred.resolve; | |
angular.element('head').append(link); | |
} | |
checkLoaded(url, deferred, tries); | |
return deferred.promise; | |
}; | |
return injectCSS; | |
}]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment