$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的更多相关文章

  1. AngularJs $templateCache 和 $templateRequest 模板缓存

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

  2. 我的angularjs源码学习之旅3——脏检测与数据双向绑定

    前言 为了后面描述方便,我们将保存模块的对象modules叫做模块缓存.我们跟踪的例子如下 <div ng-app="myApp" ng-controller='myCtrl ...

  3. [AngularJS] Taking control of your templates using $templateCache

    Using $templateCache for quickly retrieval from the cache after first time used. $templateCache main ...

  4. angularJs $templateCache

    模板加载后,AngularJS会将它默认缓存到 $templateCache 服务中.在实际生产中,可以提前将模板缓存到一个定义模板的JavaScript文件中,这样就不需要通过XHR来加载模板了 $ ...

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

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

  6. Forms in Angular 2

    Input handling is an important part of application development. The ng-model directive provided in A ...

  7. angular 缓存模板 ng-template $templateCache

    由于浏览器加载html模板是异步加载的,如果加载大量的模板会拖慢网站的速度,这里有一个技巧,就是先缓存模板. 使用angular缓存模板主要有三种方法: 方法一:通过script标签引入 <sc ...

  8. Event Binding in Angular

    https://www.pluralsight.com/guides/angular-event-binding Introduction In this guide, we will explore ...

  9. angular

随机推荐

  1. UISegmentedControl 踩坑

    @interface JLMyContactsViewController () @property (nonatomic, strong)   UIImageView *navImageView; ...

  2. Android客户端通过socket与服务器通信

    android端--Client package com.sec.chatroomandroid; import java.io.BufferedReader; import java.io.Buff ...

  3. C#获取IPv4代码

    using System; using System.Collections.Generic; using System.Collections.Specialized; using System.C ...

  4. UML关系总结

    用例图: 1.扩展关系:如果已有一个用例,在这个用例的基础(该用例是完整的)上加入新的动作形成了另一个用例,即后者是通过继承前者的属性并加入新的内容而来的,则前者通常称为通用化用例,后者常为扩展用例. ...

  5. HDU 4456(二维树状数组+坐标转换)

    题目链接:Problem - 4456 看别人叙述看的心烦,于是我自己画了一张图. 上图. 上代码 #include <iostream> #include <cstdio> ...

  6. Gradle依赖项学习总结,dependencies、transitive、force、exclude的使用与依赖冲突解决

    http://www.paincker.com/gradle-dependencies https://docs.gradle.org/current/userguide/dependency_man ...

  7. js优化与注意点

    1.使用尽可能少的全局变量.尽量用var来声明变量,避免隐式使用全局变量. 隐式全局变量和明确定义的全局变量间有些小的差异,就是通过delete操作符让变量未定义的能力. 通过var创建的全局变量(任 ...

  8. .NET的对象映射工具AutoMapper使用笔记

    AutoMapper是一个.NET的对象映射工具. 项目地址:https://github.com/AutoMapper/AutoMapper. 帮助文档:https://github.com/Aut ...

  9. 全球主流8位MCU芯片详细解剖No.2:英飞凌 XC866 - 全文

    [导读] XC866是新型8位微控制器系列(XC800)的第一代系列产品,集成高性能8051核.片内FLASH及功能强大的外设集.此外,XC800系列产品内部集成的片 内振荡器和支持3.3V或5.0V ...

  10. 全文搜索之 Elasticsearch

    概述 Elasticsearch (ES)是一个基于 Lucene 的开源搜索引擎,它不但稳定.可靠.快速,而且也具有良好的水平扩展能力,是专门为分布式环境设计的. 特性 安装方便:没有其他依赖,下载 ...