由于浏览器加载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. List泛型的应用

    在我看一个源程序的时候看到这个例子使用了IList<T>返回类型,因为上午刚刚总结过List<T>的详细用法,突然出现了IList<T>,感觉很奇怪,于是上网搜集了 ...

  2. jquery 查找已经选中的下拉框/select

    $("select[name='select_name']").find('option:selected').text(); $('#select_name option:sel ...

  3. java 企业门户网站 源码 自适应响应式 freemarker 静态引擎 html5 SSM

    官网 http://www.fhadmin.org/ 系统介绍: 1.网站后台采用主流的 SSM 框架 jsp JSTL,网站后台采用freemaker静态化模版引擎生成html 2.因为是生成的ht ...

  4. iOS10 语音播报填坑详解(解决串行播报中断问题)

    iOS10 语音播报填坑详解(解决串行播报中断问题) 在来聊这类需求的解决方案之前,咱们还是先来聊一聊这类需求的真实使用场景:语音播报.语音播报需求运用最为广泛的应该是收银对账了,就类似于支付宝.微信 ...

  5. Java多线程入门知识点梳理

    前言 在多核时代,高并发时代,对系统并行处理能力有很高要求.多线程就是这个时代最好的产物.通过使用多线程可以增强系统并行处理能力,提高CPU资源的有效利用:从而提高系统的处理能力.常见应用场景如:多窗 ...

  6. python爬虫练习 -- 签名器+GUI界面(Tkinter)

    效果图: 实现步骤如下: 实现原理:其实就是套了一层GUI的壳,主要还是爬虫抓取某个网站返回的数据,然后利用python自带的GUI工具包Tkinter来实现gui界面: 1.爬虫分析: 目标站点:h ...

  7. VS Code 的常用快捷键及插件(前端)

    一.vs code 的常用快捷键 1.注释: a) 单行注释:[ctrl+k,ctrl+c] 或 ctrl+/ b) 取消单行注释:[ctrl+k,ctrl+u] (按下ctrl不放,再按k + u) ...

  8. 关于json时间数据格式转换与修改

    使用easyui获取JSON时间数据时间数据时,通常是一长串的数字而不是我们想要的类似2018-11-01的普通时间格式. 此时我们就需要使用到关于JSON时间的格式化,而将时间转化成我们想要的格式. ...

  9. php 面试常问 基础知识

    字符串都是海针 数组是针海 输出及打印 echo 8%(-2); //输出结果为0 模后的正负号可无视 模前的有用 echo date('Y-m-d H:i:s',strtotime('-1 day' ...

  10. linux-2.6.22.6内核启动分析之配置

    配置过程最终结果是生成.config文件,我们想要对配置的目的有很清楚的了解,必须先对.config文件进行分析.通过cd命令切换到linux-2.6.22.6内核目录,输入vi .config 可以 ...