Angular: 从缓存中删除特定模板

Angular: Remove a specific template from cache

我知道如何通过

从缓存中删除所有模板
$templateCache.removeAll();

但我只想从缓存中删除特定模板。 模板不是从 $routeProvider 加载的,而是通过使用

从指令呈现的
templateUrl: 'template/page/file.html'

我的应用结构是这样的

-web
  - js
    - controllers
       - appController.js
  - templates
    - page
      - file.html

$templateCache.remove('/templates/page/file.html'); 在 appController.js

尝试在应用程序的 运行 方法中执行此操作:

 app.run([
        "$rootScope", "$templateCache", "authService", function($rootScope, $templateCache, authService) {
            $rootScope.$on("$routeChangeStart", function(event, next, current) {

                    if (typeof (current) !== "undefined") {
                        $templateCache.remove(current.templateUrl);
                    }


            });
        }
    ]);

$templateCache is based off the $cacheFactory 并且由于后者有一个 .remove(key),它也适用于您的 $templateCache。也许你拿错钥匙了?

您可以尝试致电 .get(key) 检查您是否拥有正确的密钥(我怀疑您没有 - 这就是 .remove(key) 对您不起作用的原因)。

也许您的模板文件的路径弄乱了密钥,相对路径可能不是实际的密钥,而是完整路径或单独的文件名。

问题出在我的代码上,函数触发器不正确。我通过

解决了它
$rootScope.$on('$viewContentLoaded', function() {
    $templateCache.remove('templates/page/file.html');
});

所以现在模板在加载页面时从缓存中删除。 感谢大家的帮助。