问题描述
我对$ templateCache资源有些困惑;
I'm a little confuse with the $templateCache resources;
-
html/模板将存储在服务器端吗?
the html/template will be stored on the server-side ?
还是将其存储在浏览器的内存中?如果是这样,内存限制是多少?
Or will be stored on browser memory ? If so what's the memory limits?
推荐答案
$ templateCache如何工作
当您拥有templateUrl
时,在向该模板发出网络请求之前,Angular将首先在$templateCache
中查找它是否存在.
How $templateCache Works
When you have templateUrl
, before making a network request for that template, Angular will look in $templateCache
first to see if it's there.
第一次请求某个templateUrl
网络请求时,但是结果存储在$templateCache
中,因此下次访问该URL时,将从$templateCache
中检索该URL.
The first time you request some templateUrl
a network request is made, but the result is stored in $templateCache
so the next time you go to that url, it's retrieved from $templateCache
.
在开发中,不缓存第一个请求的事实很好,因为请求是针对本地文件的请求,而不是针对外部文件的网络请求(这些请求具有较高的延迟并且非常耗时).
In development, the fact that the first request isn't cached is fine because requests are for local files, not network requests for external files (which have high latency and are time consuming).
在生产环境中,有两种设置方法,使文件最初放置在$templateCache
中.
In production, there are two ways to set it up such that files are initially placed in $templateCache
.
- 使用 ngTemplate .
- 在
run
块中手动添加到$templateCache
. 教程.
- Using ngTemplate.
- Adding to
$templateCache
manually in therun
block. Tutorial.
记忆问题
$ templateCache绝对不会从服务器访问模板;它将其存储在客户端上.至于客户端的位置,我设置了 SSCCE ,它似乎将其存储为JavaScript字符串.它似乎没有将其存储在localStorage
,sessionStorage
中或作为cookie.
Memory questions
$templateCache definitely does not access the template from the server; it stores it on the client. As for where on the client, I set up a SSCCE and it appears to store it as a JavaScript string. It doesn't appear to store it in localStorage
, sessionStorage
, or as a cookie.
这篇关于$ templateCache如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!