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
随机推荐
- FusionCharts 分类以及各个属性参数列表
<FusionCharts学习及使用笔记>之 第一篇 其实一直以来我都在有意无意的把我平常工作中遇到并解决的问题做个记录,放到我的网易博客中.但却一直没有想过如何把我使用的技术做一个系列化 ...
- python正则表达式例子说明
pattern = re.compile('<div.*?author">.*?<a.*?<img.*?>(.*?)</a>.*?<div.* ...
- 修改Python文件日志输出位置
Python logging模块介绍:http://blog.chinaunix.net/uid-26000296-id-4372063.html [root@fuel ~]# vi /var/lib ...
- 如何把mysql的ID归0?
如何把mysql的ID归0 可以归0的,truncate table + 表名 就行了
- JavaFX 2.0+ WebView /WebEngine render web page to an image
http://stackoverflow.com/questions/7796558/javafx-2-0-webview-webengine-render-web-page-to-an-image ...
- jetty compile
lifecycle cant been covered, 需要m2e-extra,在elcipse marcketplace找,不是叫做这个名. <parent>上有lifecycle问题 ...
- 已有 JS 模块化和打包方案收集
模块化方案 RequireJS AMD 方案, 常用的 define 语法, 异步加载模块, 目前很多支持: 官网 http://requirejs.org/ Github https://githu ...
- hibernate--一对多单向关联 (重点!!!)
多对一是在多的类上存在一的对象 一对多是在一的类上存在多的集合. 多的类 user.java: package com.bjsxt.hibernate; import javax.persistenc ...
- hadoop hdfs uri详解
body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...
- openstack controller ha测试环境搭建记录(五)——配置rabbitmq集群
配置rabbitmq集群的步骤非常简单,因为其本身含集群功能,参考openstack官网文档:http://docs.openstack.org/ha-guide/controller-ha-rabb ...