Angular - - $templateCache 和 $templateRequest
$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 vm = this;
var tpl = $templateCache.get('templateId.html');
tpl = $sce.trustAsHtml(tpl);
vm.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);
})
};
}());
Angular - - $templateCache 和 $templateRequest的更多相关文章
- AngularJs $templateCache 和 $templateRequest 模板缓存
$templateCache 第一次使用模板,它被加载到模板缓存中,以便快速检索.你可以直接将模板标签加载到缓存中,或者通过$templateCache服务. 通过script标签: <scri ...
- 我的angularjs源码学习之旅3——脏检测与数据双向绑定
前言 为了后面描述方便,我们将保存模块的对象modules叫做模块缓存.我们跟踪的例子如下 <div ng-app="myApp" ng-controller='myCtrl ...
- [AngularJS] Taking control of your templates using $templateCache
Using $templateCache for quickly retrieval from the cache after first time used. $templateCache main ...
- angularJs $templateCache
模板加载后,AngularJS会将它默认缓存到 $templateCache 服务中.在实际生产中,可以提前将模板缓存到一个定义模板的JavaScript文件中,这样就不需要通过XHR来加载模板了 $ ...
- AngularJS ngTemplate寄宿方式 模板缓存 $templateCache
AngularJS的指令中经常定义模板(template或templateUrl),可以选择将Html模板直接寄宿在WEB容器中,如Tomcat.IIS.Nginx.NodeJs Express,也可 ...
- Forms in Angular 2
Input handling is an important part of application development. The ng-model directive provided in A ...
- angular 缓存模板 ng-template $templateCache
由于浏览器加载html模板是异步加载的,如果加载大量的模板会拖慢网站的速度,这里有一个技巧,就是先缓存模板. 使用angular缓存模板主要有三种方法: 方法一:通过script标签引入 <sc ...
- Event Binding in Angular
https://www.pluralsight.com/guides/angular-event-binding Introduction In this guide, we will explore ...
- angular
随机推荐
- 2016大连网络赛 Weak Pair
Weak Pair Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Prob ...
- Counting Islands II
Counting Islands II 描述 Country H is going to carry out a huge artificial islands project. The projec ...
- jTDS Java连接SQL Server 2000数据库
Java连接SQL Server 2000数据库时,有两种方法: (1)通过Microsoft的JDBC驱动连接.此JDBC驱动共有三个文件,分别 是mssqlserver.jar.msutil.ja ...
- 扩展方法实现DevExpress控件校验
DevExpress控件中,如果要控件的值进行校验,需要用到DXValidationProvider控件和DXErrorProvider控件,按照正常思路,无论使用哪个控件要实现校验效果时都需要对每个 ...
- Allegro PCB -通孔焊盘制作 及Flash制作
通孔焊盘制作,比如插针封装 数值确定: mil单位 ...
- [iOS]C语言技术视频-14-指针变量高级用法(函数指针)
下载地址: 链接: http://pan.baidu.com/s/1ykyg2 密码: fg5t
- 如何给对话框中的控件发送消息呢?Windows消息分类
以博文CTabCtrl中介绍的那样,给Tab添加子对话框来显示Tab内容.那么如果这个子对话框中含有个CTreeCtrl控件,有个Button控件,我想要模拟给这两个控件发送消息,该怎么办呢?直接把给 ...
- 计算机学院大学生程序设计竞赛(2015’12) 1005 Bitwise Equations
#include<cmath> #include<cstdio> #include<cstring> #include<algorithm> using ...
- ZOJ 3940 Modulo Query
0--M对某个数字取模,相当于把0--M区间进行切割,每次暴力切割一下.结果的算的时候二分一下即可... 看了官方题解才会... #include<cstdio> #include< ...
- php smarty
摘自:http://linux.chinaitlab.com/PHP/38324.html 刚开始接触模版引擎的 PHP 设计师,听到 Smarty 时,都会觉得很难.其实笔者也不例外,碰都不敢碰一 ...