我喜欢使用带有ng-include
属性的模板。我想知道在多次使用相同URL时,这些模板是否被缓存。
<div ng-include src="'./Views/temp.html'"></div> //get request temp.html
<div ng-include src="'./Views/temp.html'"></div> //load from cache
我喜欢使用带有ng-include
属性的模板。我想知道在多次使用相同URL时,这些模板是否被缓存。
<div ng-include src="'./Views/temp.html'"></div> //get request temp.html
<div ng-include src="'./Views/temp.html'"></div> //load from cache
缓存通常被使用 - 但这不是Angular的责任,而是浏览器的责任。您可以通过运行Fiddler并查看页面加载时发生的情况来了解此事。如果您从服务器获得304结果代码,则意味着页面没有更改 - 因此将从缓存中获取。
强制刷新
唯一一种始终可靠地强制重新加载的方法是添加查询字符串,如下所示(您可以将“i”替换为任何变量,并且数字只需要是随机的-不是以前使用过的数字):
<div ng-include src="'./Views/temp.html?i=1000'"></div> // get request temp.html
<div ng-include src="'./Views/temp.html?i=1001'"></div> // force the page to load!
希望这能有所帮助!
$templateCache
中,完全忽略浏览器缓存。这样做的额外好处是减少请求数量(您甚至不必访问浏览器缓存)。缺点是在启动时运行更多的JS。有一些grunt插件可以实现这个功能(例如https://www.npmjs.org/package/grunt-angular-templates),但是自己制作也相当简单。我们使用的是maven,制作一个插件只需要大约100行代码。 - ivarni
$templateCache
服务可以实现这个功能。http://docs.angularjs.org/api/ng/service/$templateCache - Chandermani