8

我使用templateUrl它,效果很好!

app.directive('myDir', function() {
    return {
        templateUrl: 'partials/directives/template.html'
    };
};

但是...当我对这些模板进行更改时,它不会更新。在开发中这不是一个大问题,因为我知道更新了什么并且可以手动清除缓存。

但我无法清除所有用户的缓存。有没有办法做到这一点?喜欢使用 CACHE-CONTROL 元标记或类似的东西?

4

2 回答 2

2

据我所知,您有两种选择-

  1. 使用$cacheFactory服务移除旧缓存 获取模板后,Angular 会将其缓存在默认的 $templateCache 服务中

    // Please note that $cacheFactory creates a default key '$http'
    
    var cache = $cacheFactory.get('$http');
    
    // The remove() function removes a key-value pair from the cache, 
    // if it’s found. If it’s not found, then it just returns undefined.
    
    cache.remove(your url);
    
  2. 使用文件版本控制在每次更改时重命名文件 - 即,如果您的文件的第一个版本是 template-1.0.1.html,当您进行一些代码更改时将其重命名为 template-1.0.2.html 等等。这样,每次您进行一些更改时,都会下载新文件。

于 2014-10-06T11:28:07.817 回答
2

一个快速而肮脏的解决方案是在$httpProvider

app.config(function ($httpProvider) {
    $httpProvider.defaults.headers.get = {
        'Cache-Control': 'no-cache'
    };
});

我不会推荐这个。但它有效。

于 2016-12-05T12:57:39.613 回答