AngularJS: $route 究竟是如何工作的?
AngularJS: How exactly does $route work?
这是我被问到的一个面试问题:AngularJS:$route 服务究竟是如何工作的?
据我了解,$route 在 link/button/whatever 单击时加载模板,然后将 [ng-view] div/section 替换为模板的 HTML,并将其存储在内存中,并使用内存中的模板清除后续点击。
这是正确的还是 Angular 每次收到点击时都加载模板?
换句话说,Angular 模板加载一次还是每次都加载?在我看来,只加载一次会更有效率,但我不确定。任何见解表示赞赏。
我不认为你的回答一定是错的,因为一个页面有很多部分 rendering/how 它被渲染($route, $routeProvider, ngView 指令)
$route is used for deep-linking URLs to controllers and views (HTML partials). It watches $location.url() and tries to map the path to an existing route definition.
见documentation or dig into the source
编辑:添加了说明。
Angular $templateCache 的文档说
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 服务究竟是如何工作的?
据我了解,$route 在 link/button/whatever 单击时加载模板,然后将 [ng-view] div/section 替换为模板的 HTML,并将其存储在内存中,并使用内存中的模板清除后续点击。
这是正确的还是 Angular 每次收到点击时都加载模板?
换句话说,Angular 模板加载一次还是每次都加载?在我看来,只加载一次会更有效率,但我不确定。任何见解表示赞赏。
我不认为你的回答一定是错的,因为一个页面有很多部分 rendering/how 它被渲染($route, $routeProvider, ngView 指令)
$route is used for deep-linking URLs to controllers and views (HTML partials). It watches $location.url() and tries to map the path to an existing route definition.
见documentation or dig into the source
编辑:添加了说明。
Angular $templateCache 的文档说
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!!