Created
August 15, 2012 00:00
-
-
Save vojtajina/3354046 to your computer and use it in GitHub Desktop.
AngularJS: load all templates in one file
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
<script type="text/ng-template" id="one.html"> | |
<div>This is first template</div> | |
</script> | |
<script type="text/ng-template" id="two.html"> | |
<div>This is second template</div> | |
</script> |
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
var app = angular.module('test', []); | |
// HACK: we ask for $injector instead of $compile, to avoid circular dep | |
app.factory('$templateCache', function($cacheFactory, $http, $injector) { | |
var cache = $cacheFactory('templates'); | |
var allTplPromise; | |
return { | |
get: function(url) { | |
var fromCache = cache.get(url); | |
// already have required template in the cache | |
if (fromCache) { | |
return fromCache; | |
} | |
// first template request ever - get the all tpl file | |
if (!allTplPromise) { | |
allTplPromise = $http.get('all-templates.html').then(function(response) { | |
// compile the response, which will put stuff into the cache | |
$injector.get('$compile')(response.data); | |
return response; | |
}); | |
} | |
// return the all-tpl promise to all template requests | |
return allTplPromise.then(function(response) { | |
return { | |
status: response.status, | |
data: cache.get(url) | |
}; | |
}); | |
}, | |
put: function(key, value) { | |
cache.put(key, value); | |
} | |
}; | |
}); | |
app.config(function($routeProvider) { | |
$routeProvider.when('/one', {templateUrl: 'one.html'}); | |
$routeProvider.when('/two', {templateUrl: 'two.html'}); | |
}); |
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
<!DOCTYPE html> | |
<html ng-app="test"> | |
<head> | |
<title>Loading all templates in one file...</title> | |
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.js"></script> | |
<script type="text/javascript" src="fakeTemplateCache.js"></script> | |
</head> | |
<body> | |
<a href="#/one">one</a> | <a href="#/two">two</a> | |
<div ng-view></div> | |
</body> | |
</html> |
is there any problem if i use this code
app.run(function($http, $templateCache){
$http.get('all-templates.html').then(function(response) {
var $html = $('<div />',{html:response.data});
var elements=$html.find('script');
angular.forEach(elements,function(element){
$templateCache.put(element.id,element.innerHTML);
});
});
});
Awesome!!
hello guys...i am facing with one problem in ionic2...i want linked index.html page with second page of app..how to do it???
In actual have an svg iamge n second page of ionic2 which should be fill with color on click, by accessing index.html file
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for the code!
I just patch it in case partial.tmpl doesn't exist (like in a environment development)