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 ...
随机推荐
- 「bzoj 4025: 二分图」
题目 显然二分图没有奇环 于是考虑使用并查集维护一下看看是否存在奇环 我们可以考虑加权并查集,维护出\(x\)到\(fa_x\)的实际距离 由于我们只需要考虑奇偶性,于是我们处理出到根的路径异或一下就 ...
- POJ 1320 Street Numbers 【佩尔方程】
任意门:http://poj.org/problem?id=1320 Street Numbers Time Limit: 1000MS Memory Limit: 10000K Total Su ...
- 多条件查询接收很多参数的时候要用Map接收。
好处是,以后修改查询条件的时候不用从接口,到实现类,到controller的参数都要修改, 假如加一个查询条件,只需要前端多传一个参数值,都用map<>键值对接收,只需要在 service ...
- 【luogu P1879 [USACO06NOV]玉米田Corn Fields】 题解
题目链接:https://www.luogu.org/problemnew/show/P1879 状压DP. 设dp[i][j]表示第i行,状态为j的方案数 初始dp[0][0] = 1 这样一共12 ...
- FCC Truncate a string 解决方法
三行搞定 function truncate(str, num) { ab = str.length >num?num>3?str.slice(0,num-3)+ "...&qu ...
- 闲来无事做了一个项目,内有redis,EasyUI样式简单应用,七层分页查询,API跨域。
<link href="~/jquery-easyui-1.5.3/themes/default/easyui.css" rel="stylesheet" ...
- File、Paths和Files类的使用详解
Paths:通过get()方法返回一个Path对象,Path用于表示文件路径和文件. Files:提供了大量处理文件的方法,例如文件复制.读取.写入,获取文件属性.快捷遍历文件目录等..... Fil ...
- 在linux系统中用docker搭建ss
首先呢,你的先有一台自己的服务器把,这个就不多赘述了,我自己买了一台国外的VPS. 一.docker的安装 首先我们来看下服务器的版本信息: cat /etc/redhat-release //Cen ...
- 利用wireshark抓取TCP的整个过程分析。
原文地址:https://www.cnblogs.com/NickQ/p/9226579.html 最近,已经很久都没有更新博客了.看看时间,想想自己做了哪些事情,突然发现自己真的是太贪心,到头来却一 ...
- 【转】ruby rake执行rspec
RSpec 是Ruby的一个行为驱动开发(BDD)工具,当前的版本是 2.10.根据其入门文档,安装好之后,可以使用 rspec 命令来运行“测试”.但在某些情况下,如果参数较多,使用该命令并不方便: ...