admin管理员组文章数量:1276780
I need to cache some HTML files when my Angular controller initializes.
According to Angular $templateCache documentation I can add HTML templates to Angular with:
$templateCache.get('templateId.html')
But I can't get this to work. I've tried to get the template file within the controller and within the module run() function (Plunker). But I can see in the network console that the template is not fetched.
app.run(function($templateCache) {
$templateCache.get('templ.html');
});
What am I doing wrong?
I need to cache some HTML files when my Angular controller initializes.
According to Angular $templateCache documentation I can add HTML templates to Angular with:
$templateCache.get('templateId.html')
But I can't get this to work. I've tried to get the template file within the controller and within the module run() function (Plunker). But I can see in the network console that the template is not fetched.
app.run(function($templateCache) {
$templateCache.get('templ.html');
});
What am I doing wrong?
Share Improve this question asked Jul 15, 2014 at 12:11 HoffZHoffZ 7,7296 gold badges39 silver badges40 bronze badges 1- 1 I misunderstood the documentation. I thought the get() was "HTTP GET this template URL". But it's just a getter method. To feed the $templateCache with a template with HTTP GET, see the selected answer. – HoffZ Commented Jul 15, 2014 at 13:47
2 Answers
Reset to default 8You have to fetch html using http request then you can store it in template cache. For example:
$http.get('templ.html', {
cache: $templateCache
}).then(function(result) {
console.log(result);
});
Updated plunker code here
You can use $templateRequest to fetch the template.
The $templateRequest service runs security checks then downloads the provided template using $http and, upon success, stores the contents inside of $templateCache. If the HTTP request fails or the response data of the HTTP request is empty, a $pile error will be thrown (the exception can be thwarted by setting the 2nd parameter of the function to true). Note that the contents of $templateCache are trusted, so the call to $sce.getTrustedUrl(tpl) is omitted when tpl is of type string and $templateCache has the matching entry.
Documentation: https://docs.angularjs/api/ng/service/$templateRequest
本文标签: javascriptHow to use AngularJS templateCacheget()Stack Overflow
版权声明:本文标题:javascript - How to use AngularJS $templateCache.get()? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741279948a2369949.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论