这是我被问到的一个面试问题:AngularJS: $route 服务到底如何工作?
据我了解, $route 在链接/按钮/任何点击时加载模板,然后用模板的 HTML 替换 [ng-view] div/部分,将其存储在内存中,并用模板刷出后续点击内存中。
这是正确的还是 Angular 在每次收到点击时都会加载模板?
换句话来说,Angular 是加载模板一次还是每次都加载?在我看来,只加载一次会更有效,但我不确定这一点。任何见解都值得赞赏。
最佳答案
$templateCache 的 Angular 文档说
The first time a template is used, it is loaded in the template cache for quick retrieval
Angular $route 在内部使用 $templateCache,因此,模板会加载一次,并在后续访问同一路由时从缓存中重用。
这个blog post有更多信息。相关内容:
the first time the template is accessed, angular will get it from the server and add it to the $templateCache
every time that ID is referenced from then on, angular gets it from the $templateCache and not from the server!!
关于AngularJS:$route 到底是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31846277/