Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 部署后清除模板缓存_Angularjs - Fatal编程技术网

Angularjs 部署后清除模板缓存

Angularjs 部署后清除模板缓存,angularjs,Angularjs,我想确保我的用户在部署后看到我的Angular应用程序的最新版本。在这种情况下,清除模板缓存的正确策略是什么 我在app.run中读到了一些你可以做的$templateCache.removeAll(),但我想这会完全禁用缓存吗 您需要编写一个拦截器并“捕获”所有模板请求-您应该能够在此拦截器中添加一个URL参数,该参数将对模板资产进行版本设置 例如: // App .config() $httpProvider.interceptors.push(function($templateCache

我想确保我的用户在部署后看到我的Angular应用程序的最新版本。在这种情况下,清除模板缓存的正确策略是什么


我在
app.run
中读到了一些你可以做的
$templateCache.removeAll()
,但我想这会完全禁用缓存吗

您需要编写一个拦截器并“捕获”所有模板请求-您应该能够在此拦截器中添加一个URL参数,该参数将对模板资产进行版本设置

例如:

// App .config()
$httpProvider.interceptors.push(function($templateCache) {
  return {
    'request' : function(request) {
      if($templateCache.get(request.url) === undefined) { // cache miss
          // Item is not in $templateCache so add our query string
          request.url = request.url + '?appVersion=' + appService.getConfig().version;
      }
      return request;
  }
};
当然,每次部署新版本时,您都需要更新此应用程序配置(通过生成任务,您可以自动更新此文件)。您可能需要添加额外的检查(例如,如果URL路径以“.html”结尾,则进行比较),以确保您没有收到实际的HTTP请求


这种方法的缺点是,有时您可能有一个尚未更新的模板,您不希望浏览器缓存被破坏。如果这是您的情况,那么您应该为每个添加某种类型的md5(templateContent)——这也可以在构建时通过Grunt/Gulp实现。

只需添加检查
If(request.url.endsWith(“.html”){…}
就可以了!我为这个问题尝试了很多解决方案,这一个对我的用例来说是最好的。@hmartos请小心,.endsWith在所有浏览器和平台上都不受支持。