$templateCache

第一次使用模板,它被加载到模板缓存中,以便快速检索。你可以直接将模板标签加载到缓存中,或者通过$templateCache服务。

通过script标签

<script type=”text/ng-template” id=”template.html”>

<p>This is the content of the template</p>

</script>

备注:script标签模板不需要包含在文档头部。但他必须在$rootElement下,不然模板将会被忽略。

通过$templateCache服务:

  <div ng-app="Demo" ng-controller="testCtrl as ctrl">
<!--<div ng-include="'templateId.html'"></div>-->
<div ng-bind-html="ctrl.text"></div>
</div>
  (function () {
angular.module("Demo", [])
.run(["$templateCache",templateCache])
.controller("testCtrl", ["$templateCache","$sce",testCtrl]);
function templateCache($templateCache){
$templateCache.put('templateId.html', '<a>This is the content of the template</a>');
}
function testCtrl($templateCache,$sce) {
var tpl = $templateCache.get('templateId.html');
tpl = $sce.trustAsHtml(tpl);
this.text = tpl;
};
}());

在上面调用模板的代码中,可以使用controller里的代码调用缓存里的模板,但是需要注意的是,需要使用$sce转成受信任的html插入代码,所以这里需要注入$sce服务。而且这边不止可以使用js调用,也可以直接在html里标签里使用ng-include调用。

$templateRequest

$templateRequest服务运行进行安全检测,然后使用$http下载被提供的模板,成功后,将内容存储在$templateCache里。如果HTTP请求失败或HTTP请求的响应数据是空的,将抛出个$compile错误(通过设置该函数的第二个参数为true)。该注意的是,$templateCache的内容是可信的,所以调用$sce.getTrustedUrl(tpl)是省略的,当tpl的类型是字符串并且$templateCache具有匹配的项。

使用:$templateRequest(tpl,[ignoreRequestError]);

tpl:字符串或者TrustedResourceUrl,HTTP请求URL的模板。

ignoreRequestError:boolean值,当请求失败或模板为空时,是否忽略该异常。

使用代码:

  (function () {
angular.module("Demo", [])
.run(["$templateCache",templateCache])
.controller("testCtrl", ["$templateRequest","$sce",testCtrl]);
function templateCache($templateCache){
$templateCache.put('templateId.html', '<a>This is the content of the template</a>');
}
function testCtrl($templateRequest,$sce) {
var vm = this;
$templateRequest("templateId.html").then(function(html){
vm.text = $sce.trustAsHtml(html);
})
};
}());

AngularJs $templateCache 和 $templateRequest 模板缓存的更多相关文章

  1. AngularJS ngTemplate寄宿方式 模板缓存 $templateCache

    AngularJS的指令中经常定义模板(template或templateUrl),可以选择将Html模板直接寄宿在WEB容器中,如Tomcat.IIS.Nginx.NodeJs Express,也可 ...

  2. Angular - - $templateCache 和 $templateRequest

    $templateCache 第一次使用模板,它被加载到模板缓存中,以便快速检索.你可以直接将模板标签加载到缓存中,或者通过$templateCache服务. 通过script标签: <scri ...

  3. AngularJs 禁止模板缓存

    因为AngularJs的特性(or 浏览器本身的缓存?),angular默认的HTML模板加载都会被缓存起来.导致每次修改完模板之后都得经常需要清除浏览器的缓存来保证浏览器去获得最新的html模板,自 ...

  4. [ZZ] D3D中的模板缓存(3)

    http://www.cppblog.com/lovedday/archive/2008/03/25/45334.html http://www.cppblog.com/lovedday/ D3D中的 ...

  5. D3D 模板缓存的创建过程

    下面是我对模板缓存创建的理解: 1. 模板缓存是和深度缓存一起被创建的,将深度缓存的一部分作为模板缓存使用. 深度缓存和模板缓存是在Direct3D初始化时创建的,D3DPRESENT_PARAMET ...

  6. Jquery 模板插件 jquery.tmpl.js 的使用方法(2):嵌套each循环,temp调用(使用预编译的模板缓存)

    直接上代码吧 一:主窗口 /*#region SendChooseTargetTemplate 发送候选人主窗口模板*/ var SendChooseTargetTemplate = ''; Send ...

  7. [Java] Spring boot2 整合 Thymeleaf 后 去除模板缓存

    Spring boot2 整合 Thymeleaf 后 去除模板缓存 网上好多文章只是简单粗暴的说,在 application.properties  做如下配置即可: #Thymeleaf cach ...

  8. windows+linux开发环境 解决laravel blade模板缓存问题

    编码环境windows10 编码IDE:phpstorm 2016.2 PHP框架:laravel5.3 + 代码运行环境:centos7 + nginx 在开发过程中,上传blade模板文件到lin ...

  9. $smary模板缓存

    <?php //引入配置文件 $fillname="../cache/testhuancun.html"; //设置一个缓存时间 $time=; //判断如果缓存文件不存在的 ...

随机推荐

  1. 什么是API

    我们从API的功能.分类.设计.实现.用户来看什么是API. API是应用程序组件之间通信的接口 --wiki:Application Programming Interface In compute ...

  2. C# WebApi Xml序列化问题解决方法:“ObjectContent`1”类型未能序列化内容类型“application/xml;charset=utf-8"的响应正文。...

    在调试一个WebApi程序时,出现下面错误: 通过分析怀疑是未添加序列化属性引起的,实体类改为下面结构后,问题依旧: 通过查阅资料和不断尝试,修改实体类的属性注解搞定:

  3. HDU2389-Rain on your Parade-二分图匹配-ISAP

    裸二分图匹配 /*--------------------------------------------------------------------------------------*/ #i ...

  4. 树莓派Odroid等卡片式电脑上搭建NAS教程系列5-Samba服务器安装

    本文章首发于浩瀚先森博客,地址: http://www.guohao1206.com/2016/08/23/967.html samba时一款为了实现linux系统中的文件能在windows系统中正常 ...

  5. CUDA1.1-函数类型限定符与变量类型限定符

    这部分来自于<CUDA_C_Programming_Guide.pdf>,看完<GPU高性能变成CUDA实战>的第四章,觉得这本书还是很好的,是一种循序渐进式的书,值得看,而不 ...

  6. ASP.NET 系列:单元测试之StructureMap

    ASP.NET使用StructureMap等依赖注入组件时最重要就是EntityFramework的DbContext对象要保证在每次HttpRequest只有一个DbContext实例,这里将使用第 ...

  7. Web Audio API 实现音频可视化

    声明:本文为原创文章,如需转载,请注明来源WAxes,谢谢! 一转眼就已经有三个月没写博客了,毕业季事情确实多,现在也终于完全毕业了,博客还是不能落下.偶尔还是要写一下. 玩HTML5的Audio A ...

  8. java文件cmd运行出现中文乱码

    今天刚开始学java,使用cmd命令执行java文件的时候,发现中文打出来是一串乱码. 于是就百度了一下,发现一个行之有效的方法. 首先使用命令:javac -encoding utf-8 Hello ...

  9. php 升级php5.5

    rpm -Uvh http://mirror.webtatic.com/yum/el6/latest.rpm yum install php55w php55w-opcache yum install ...

  10. Java Little Knowledge

    1.Constructor running order of Base class and Derived class This is Alibaba's audition problem. clas ...