简体   繁体   English

预取$ templateCache

[英]Prefetching $templateCache

My angularjs application basically loads in 4 steps: 我的angularjs应用程序基本上按4个步骤加载:

  • main HTML 主要HTML
  • all scripts and CSS files 所有脚本和CSS文件
  • $templateCache , let's call it template.html $templateCache ,我们称它为template.html
  • images 图片

I guess, I should bypass the $templateCache when loading the starting page. 我想,加载起始页面时应该绕过$templateCache But for now, I'd like to start fetching template.html ASAP, ie, at the same time as the scripts get loaded. 但是现在,我想开始尽快获取template.html ,即,在加载脚本的同时。

The possibly relevant piece of my code looks like 我的代码中可能相关的部分看起来像

<script src='//ajax.googleapis.com/.../angular.min.js'></script>
<script src='/my.js'></script>
<link href='/my.css' media='all' rel='stylesheet'/>

Here, my.js contains all my scripts and gets loaded at the same time as angular. 在这里, my.js包含了我所有的脚本,并与angular同时被加载。 Is there something allowing me to fetch template.html in the same way, so it gets put into the browser's cache? 是否可以通过相同的方式获取template.html ,以便将其放入浏览器的缓存中?

Update 更新资料

I've tried two prefetching possibilities, both failed: 我尝试了两种预取可能性,但都失败了:

<link href="template.html" rel="prefetch"/>

and

<iframe class=invisible src="template.html"></iframe>

The first gets cancelled and the second comes too late to be effective. 第一个被取消,第二个来不及生效。

As the HTML can't be fetch soon enough, the whole script directive is completely pointless. 由于无法足够快地获取HTML,因此整个脚本指令完全没有意义。

According to runTarm's suggestion, I ended up generating the Javascript instead. 根据runTarm的建议,我最终生成了Javascript。 All it takes is 它所要做的就是

.factory('$templateCache', function($cacheFactory, $http, $injector) {
    var cache = $cacheFactory('templates');
    cache.put("MY_PAGE_1", "ESCAPED_TEXT_1");
    cache.put("MY_PAGE_2", "ESCAPED_TEXT_2");
    ...
    return {
        get: function(url) {
            var fromCache = cache.get(url);
            return fromCache ? fromCache : $http.get(url);
        }
    };
}

In the escaped text you must honor the 1024 string-literal length limit and escape newlines and double quotes. 在转义的文本中,您必须遵守1024个字符串的字面长度限制,并转义换行符和双引号。 That's all. 就这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM