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

<script type="text/ng-template" id="hello.html">
<h4>hello</h4>
<p>这是script标签获取模板文件的方式</p>
<a href="http://www.baidu.com">标签启用templateCache方式</a>
</script>

注意,这里的id只是指十几使用模板时的一个引用,而不是一个url, 模板的实际内容在script标签里。ng-template指明这是angular模板。 
当然,模板的实际内容我们也可以使用themeleaf的标签来引用一些html页面,如th:include和th:replace。

<script type="text/ng-template" id="hello.html">
<div th:replace="components/header :: header"></div> //<div th:include="components/footer :: footer"></div>
</script>

th:replace和th:include的参数格式是:templatename::[domselector]表示引入模板页面中的某个模块。 
其中templatename引入模板页面,domselector引入自身模板的模块。 
注意:script标签模板不需要包含在文档头部。但他必须在$rootElement下,不然模板将会被忽略。

方法二:通过$templateCache服务来实现

angular.module('myApp', [])
.controller('myCtrl', ['$scope','$templateCache', function($scope,$templateCache){
var tmp = '<h4>hello</h4>'
+ '<p>这是直接调用$templateCache服务获取模板文件的方式</p>'
+ '<a href="http://www.baidu.com">服务启用templateCache方式</a>';
$templateCache.put('hello.html',tmp);
}])

$templateCache的put()方法负责向内存中写入模板内容,然后在directive中用controllerUrl:'hello.html'就可以使用,甚至可以使用ng-include="'hello.heml'"来调用。 
ng-include的方式:

<div ng-app="myApp" ng-controller="myCtrl as ctrl">
<div ng-include="'hello.html'"></div>
</div>

directive的方式:

angular.module('myApp', [])
.directive('templateDemo', ['$log', function($log){
return {
restrict: 'A',
templateUrl: 'hello.html',
replace: true,
link: function($scope, iElm, iAttrs, controller) {}
}
}]) //html
<div ng-app="myApp" ng-controller="myCtrl as ctrl">
<div template-demo></div>
</div>

$templateCache基于cacheFactory,接口保持一致,方法主要有:

方法 说明
put 向内存写入模板内容
get 从内存获取模板内容
remove 传入key值,删除对应模板内容
removeAll 删除所有模板内容
destroy 解除key-value对应关系,但不释放内存
info 模板缓存对象的信息

方法三:$templateCache和ng-bind-html

<div ng-app="Demo" ng-controller="testCtrl as ctrl">
<div ng-bind-html="ctrl.text"></div>
</div>
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;
};
}

注意:使用ng-bind-html就要使用\$sce转成受信任的html插入代码。

angular 缓存模板 ng-template $templateCache的更多相关文章

  1. --@angularJS--模板加载之缓存模板demo

    不用缓存模板的写法如下: 1.index.html: <!DOCTYPE HTML><html ng-app="app"><head>    & ...

  2. 用12个例子全面示范Angular的模板语法

    template分支,用12个例子全面示范Angular的模板语法 // 使用方法 git clone https://git.oschina.net/mumu-osc/learn-component ...

  3. Angular - Templates(模板)

    点击查看AngularJS系列目录 转载请注明出处:http://www.cnblogs.com/leosx/ 在Angular中,模板是一个包含了Angular特定元素和属性的HTML.Angula ...

  4. 模板层template

    继续之前的views,你可 能已经注意到我们例子中视图中返回的的方式有点特别.也就是说.HTML被直接硬编码在Python代码之中 def current_datetime(request): now ...

  5. angular 2 - 001 ng cli的安装和使用

    angular cli 创建项目和组件 ng new my-app --skip-install cd my-app cnpm install ng serve localhost:4200 angu ...

  6. DjangoMTV模型之视图层views及模板层template

    Django视图层中views的内容 一个视图函数,简称视图,是一个简单的Python 函数,它接受Web请求并且返回Web响应.响应可以是一张网页的HTML内容(render),也可以是一个重定向( ...

  7. dj 模板层template

    1 模板语法之变量 在 Django 模板中遍历复杂数据结构的关键是句点字符, 语法: {{var_name}} def index(request): import datetime s=" ...

  8. c# 一种缓存模板

    在很多项目中,尤其是服务端,我们需要临时缓存一些数据,对于完整的我就不说了.主要的保持方法有: 1.大型数据库 2.缓存组件 3.文件(按照自己定义的格式存储) 4.一些缓存数据库(sqlte,h2, ...

  9. ionic准备之angular基础——模板引入(7)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

随机推荐

  1. Linux下utf-8 BOM 的检查和删除 (65279错误解决办法)

    Java代码在转换为UTF-8编码后,Eclipse编译运行没有问题,但是用Maven编译时,抛出非法字符65278错误. 原因在于,转换后文件头部带有BOM信息,而Maven不支持,删掉文件头的BO ...

  2. nowcoder模拟赛

    R1 D1 普及组... T1/T2 咕 T3 链接:C 小A有一个只包含左右括号的字符串S.但他觉得这个字符串不够美观,因为它不是一个合法的括号串.一个合法的括号串是这样定义的: ()是合法的括号串 ...

  3. P3177 [HAOI2015]树上染色

    题目描述 有一棵点数为 N 的树,树边有边权.给你一个在 0~ N 之内的正整数 K ,你要在这棵树中选择 K个点,将其染成黑色,并将其他 的N-K个点染成白色 . 将所有点染色后,你会获得黑点两两之 ...

  4. web-ctf随机数安全

    rand() 函数在产生随机数的时候没有调用 srand(),则产生的随机数是有规律可询的. 产生的随机数可以用下面这个公式预测 : state[i] = state[i-3] + state[i-3 ...

  5. ethereumjs/ethereumjs-vm-2-API文档

    https://github.com/ethereumjs/ethereumjs-vm/blob/master/docs/index.md vm.runBlockchain Processes blo ...

  6. pprint的惊喜

    因为我个人经常喜欢打印dir来看模块的方法,每次都是for循环换行,这个真好用 import pprint pprint.pprint (dir(pprint)) 执行下面的代码,会发现,没有自动换行 ...

  7. 安装Oracle 18.4 RAC 遇到ORA-40238报错

    场景: Aix 7.2 上安装Oracle 18.4 RAC 执行root.sh脚本,ASM failed to start !查看日志文件:ORA-40238: invalid linear alg ...

  8. Oracle11g 行列转换函数PIVOT and UNPIVOT

    作为Oracle开发工程师,推荐大伙看看 PIVOT and UNPIVOT Operators in Oracle Database 11g Release 1 This article shows ...

  9. iOS:手势与矩形、点运算相关(18-01-24更)

    1.矩形.点运算 1.获取当前的View在Window的frame 2.包含判断 3.获取点击在响应者 touchesBegan 的位置 4.UIScrollView.UITableView 实时 位 ...

  10. 初试mininet(可选PyCharm)

    目录 0x00 Mininet 0x01 Important classes, methods, functions 0x02 Sample 0x04 run in shell 0x05 Output ...