Skip to content

Instantly share code, notes, and snippets.

@dhigginbotham
Created January 18, 2014 03:41
Show Gist options
  • Save dhigginbotham/8485859 to your computer and use it in GitHub Desktop.
Save dhigginbotham/8485859 to your computer and use it in GitHub Desktop.
Angular.js factory to load preload templates.
app.factory('$preload', ['$templateCache', '$http',
function ($templateCache, $http) {
// angular.js preload template factory, the purpose of this
// is to preload all of your applications templates, once.
// all callbacks should fire with (err, data) node.js style
// flow.
var internalHttpRequest = function (url, fn) {
$http.get(url).success(function (data) {
return fn(data);
});
};
var preload = function (templates, fn) {
var templates = templates || [];
if (templates.length) {
templates.forEach(function (template) {
internalHttpRequest(template, function (html) {
$templateCache.put(template, html);
});
});
return fn(null, true);
} else {
return fn(true, null);
}
};
return preload;
}]);
@fercarvo
Copy link

How do I get all URLs templates?
You use an array of URLs...

I'm doing this but I have too many.

$http.get('views/asistente/historial.html', { cache: $templateCache }).then(() => { }, error)
$http.get('views/asistente/historial.pedidointerno.html', { cache: $templateCache }).then(() => { }, error) $http.get('views/asistente/historial.recetas.html', { cache: $templateCache }).then(() => { }, error) $http.get('views/asistente/historial.transferencias.html', { cache: $templateCache }).then(() => { }, error)
...
..
.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment