AngularJS:$route 到底是如何工作的?

标签 angularjs ngroute

这是我被问到的一个面试问题: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/

相关文章:

javascript - 取消设置对象属性

jquery - 如何使用 AngularJS NgRoute 启动 MixItUp

javascript - angularjs - ngRoute 无法正常工作

javascript - 具有多个模块的路由和内联 View 不起作用 - $http 拦截器可能会导致错误

javascript - AngularJS:访问 $routeProvider 中的范围变量

javascript - 为 Angular 设置 d3-tip

javascript - Cordova 应用程序从头开始,关于 ngCordova、Ionic 和 Typescript 的决定

javascript - AngularJs 用户验证对每个字符发出请求

javascript - 如何将 ngRoute 注入(inject) Jasmine/Karma AngularJS 单元测试中?

angularjs - 将本地 Node JS HTTP 服务器端口(在 Electron 中运行)传递给 angular.service