angular 缓存模板 ng-template $templateCache
由于浏览器加载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的更多相关文章
- --@angularJS--模板加载之缓存模板demo
不用缓存模板的写法如下: 1.index.html: <!DOCTYPE HTML><html ng-app="app"><head> & ...
- 用12个例子全面示范Angular的模板语法
template分支,用12个例子全面示范Angular的模板语法 // 使用方法 git clone https://git.oschina.net/mumu-osc/learn-component ...
- Angular - Templates(模板)
点击查看AngularJS系列目录 转载请注明出处:http://www.cnblogs.com/leosx/ 在Angular中,模板是一个包含了Angular特定元素和属性的HTML.Angula ...
- 模板层template
继续之前的views,你可 能已经注意到我们例子中视图中返回的的方式有点特别.也就是说.HTML被直接硬编码在Python代码之中 def current_datetime(request): now ...
- angular 2 - 001 ng cli的安装和使用
angular cli 创建项目和组件 ng new my-app --skip-install cd my-app cnpm install ng serve localhost:4200 angu ...
- DjangoMTV模型之视图层views及模板层template
Django视图层中views的内容 一个视图函数,简称视图,是一个简单的Python 函数,它接受Web请求并且返回Web响应.响应可以是一张网页的HTML内容(render),也可以是一个重定向( ...
- dj 模板层template
1 模板语法之变量 在 Django 模板中遍历复杂数据结构的关键是句点字符, 语法: {{var_name}} def index(request): import datetime s=" ...
- c# 一种缓存模板
在很多项目中,尤其是服务端,我们需要临时缓存一些数据,对于完整的我就不说了.主要的保持方法有: 1.大型数据库 2.缓存组件 3.文件(按照自己定义的格式存储) 4.一些缓存数据库(sqlte,h2, ...
- ionic准备之angular基础——模板引入(7)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
随机推荐
- 在64位Ubuntu上编译32位程序常见错误
问 题1: 找不到头文件 asm/errno.h 解决办法 : [/usr/lib/gcc$ ]sudo ln -s x86_64-linux-gnu/asm asm 问题2:找不到gcc ...
- Linux系统调用原理
操作系统通过系统调用为运行于其上的进程提供服务. 当用户态进程发起一个系统调用, CPU 将切换到 内核态 并开始执行一个 内核函数 . 内核函数负责响应应用程序的要求,例如操作文件.进行网络通讯或者 ...
- 离线服务器下docker的部署与应用
一分钟内形成docker的模糊概念 网上很多文章避免将docker与虚拟机混为一谈,但对于初学者来说,完全可以将docker当做一种虚拟机技术,只需要牢牢记住一点最重要的区别:docker依赖于物理机 ...
- StackStorm利用CORS null origin获得RCE (CVE-2019-9580)
在2.10.3/2.9.3之前,如果请求的来源未知,我们将返回null,null可以导致某些客户端中来自未知来源的成功请求,允许针对StackStorm API进行XSS样式攻击. (Firefox上 ...
- C++笔记020:const 和 #define 的对比
原创笔记,转载请注明出处! 点击[关注],关注也是一种美德~ 第一,const与#define的相同点 C++中的const常量类似于宏定义 const int c = 5 ≍ #defi ...
- Ubuntu install 错误 E:Unable to locate package
今天在 Ubuntu 上执行 sudo apt install sl 命令,结果报错:E:Unable to locate package sl 上网查询了一下,先更新一下 apt-get,执行:su ...
- Web前端---HTTP协议
目录 HTTP协议 一.http协议概述 二.http请求报文 1.GET请求 2.POST请求 三.http响应报文 1.响应报文内容 2.状态码(Status Code) HTTP协议 一.htt ...
- mysql事件关闭解决办法
Mysql 事件event_scheduler是OFF 开启 Event Scheduler,以下4种方式等效 SET GLOBAL event_scheduler = ON; SET @@globa ...
- vdbench 数据校验测试方法
[root@dntosu001 vdbench]# cat pbd.conf*SD: Storage Definition*WD: Workload Definition*RD: Run Defini ...
- python实现用户登录问候
创建一个至少包含 5个用户名的列表,且其中一个用户名为 'admin' .想象你要编写代码,在每位用户登录网站后都打印一条问候消息.遍历用户名列表,并向每位用户打印一条问候消息.添加一条 if 语句, ...