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
随机推荐
- python标准库-日志logging
1.模块级别 先看一下logging模块的日志级别特点,共分6个等级. 可以手工设置当前日志的默认等级(warn),当日志输出的等级高于默认等级时,日志输出到屏幕,否则不输出. #!/usr/bin/ ...
- excel 合并多个文件
新建一个工作表,命名后保存到和与合并的100个文件同一个文件文件夹,摁 alt + f11,双击工程资源管理器里面的sheet1(sheet1),在右侧的代码区粘贴如下代码.运行.等候一会就OK了. ...
- js中的 AOP
原文 :http://blog.csdn.net/notejs/article/details/8770575 面向切面的编程(AOP)还是有点意思的,可以在不修改原有代码的情况下增加新功能.有一些j ...
- highcharts 去掉打印和链接
1)去掉或更改图片右下角的链接 在highcharts.js文件中搜索credits字符串,找到如下的字符串, enabled:设置是否显示链接 text:设置链接显示的名称 href:设置链接的ur ...
- mysql 触发器学习
1. 一个简单的例子 1.1. 创建表: create table t(s1 integer); 1.2. 触发器: delimiter | create trigger t_trigger befo ...
- Cocos2dx 学习笔记整理----第一个项目
接上一节, 进入新建的项目下面的proj.win32下面,找到项目名.sln文件(我的是game001.sln),双击会通过VS2010打开.(当然,你装了VS什么版本就是什么版本) 将你的项目设为启 ...
- POJ 2686 Traveling by Stagecoach
状压DP dp[s][p]用了哪几张票,到哪个节点的最小费用. 注意:G++ %.3lf输出会WA,但C++能过:改成%.3f,C++,G++都能AC #include<cstdio> # ...
- [iOS Animation]-CALayer 变换
变换 很不幸,没人能告诉你母体是什么,你只能自己体会 -- 骇客帝国 在第四章“可视效果”中,我们研究了一些增强图层和它的内容显示效果的一些技术,在这一章中,我们将要研究可以用来对图层旋转,摆放或者扭 ...
- linux 驱动入门6
看/sys目录经常看到bus device driver class. 这也是网上大量说的驱动驱动模型.这些的关系得熟悉得明白吧.是的.今天我先不整他们的关系.先逐个击破,然后再统一来理清楚他们之间的 ...
- 23、手把手教你Extjs5(二十三)模块Form的自定义的设计[2]
在本节中将要加入各种类型的字段,在加入字段的时候由于可以一行加入多个字段,因此层次结构又多了一层fieldcontainer.form里面的主要层次结构如下: form -- fieldSet -- ...