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

时间:2016-12-28 14:12:56

标签: javascript angularjs angular-ui-bootstrap

我知道如何通过

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

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

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

我的app结构就是这个

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

我在appController.js

中做了$templateCache.remove('/templates/page/file.html');

3 个答案:

答案 0 :(得分:1)

$templateCache基于$cacheFactory,由于后者有.remove(键),因此它也适用于$ templateCache。也许你拿错了钥匙?

您可以尝试拨打.get(key)来检查您是否拥有正确的密钥(我怀疑您没有 - 这就是.remove(key)无法为您工作的原因)。

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

答案 1 :(得分:1)

问题在于我的代码,函数触发器不正确。

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

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

答案 2 :(得分:0)

尝试在app的run方法中执行此操作:

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

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


            });
        }
    ]);