angular学习笔记(三十)-指令(3)-templateUrl
这篇主要介绍指令中的templateUrl属性:
templateUrl属性值是一个url路径,路径指向一个html模板,html模板会填充(或替换)指令内容:
比如上一篇文章里的案例,我们把原来的template属性改用为templateUrl属性:
方法一:
html:
<!DOCTYPE html>
<html ng-app = 'dirAppModule'>
<head>
<title>20.2 指令(templateUrl)</title>
<meta charset="utf-8">
<script src="angular.js"></script>
<script src="script.js"></script>
<style type="text/css">
*{
font-family:'MICROSOFT YAHEI';
font-size:12px
}
h3 {
color: #CB2027;
}
</style>
</head>
<body>
<div>
<cd-hello></cd-hello>
<div cd-hello></div>
<div class="cd-hello"></div>
</div>
</body>
</html>
js:
var dirAppModule = angular.module('dirAppModule',[]);
dirAppModule.directive('cdHello',function(){
return {
restrict:'EAC',
replace:true,
templateUrl:'hello.html'
}
});
hello.html:
<h3>hi,code_bunny</h3>
这样得到的结果和使用template:<h3>hi,code_bunny</h3>得到的结果是一致的.

*以这种方式使用templateUrl属性必须在环境中运行,本地文件是不行的.
方法二:
上面这种方式加载模板,在模板文件比较大,加载需要一定时间的情况下,用户可能先看到标识符,等待模板加载完后才看到内容.如果要避免这种情况,可以使用以下方法:
html:
<!DOCTYPE html>
<html ng-app="dirAppModule">
<head>
<title>20.3指令</title>
<meta charset="utf-8">
<script src="../angular.js"></script>
<script type="text/ng-template" id="hello.html">
<h3>hi,code_bunny</h3>
</script>
<script src="script.js"></script>
</head>
<body>
<cd-hello></cd-hello>
<div cd-hello></div>
<div class="cd-hello"></div>
</body>
</html>
js:
/*20.3 指令 */
var dirAppModule = angular.module('dirAppModule',[]);
dirAppModule.directive('cdHello',function(){
return {
restrict:'EAC',
templateUrl:'hello.html',
replace:true
}
});
直接在页面里添加一个script标签,script的type属性为:text/ng-template, script的id属性值自己定义,templateUrl的值就是这个script标签的id属性值,比如这个例子中的hello.html
注意这个script标签是不会发送http请求的.
方法三:
html:
<!DOCTYPE html>
<html ng-app="dirAppModule">
<head>
<title>20.4指令</title>
<meta charset="utf-8">
<script src="../angular.js"></script>
<script src="script.js"></script>
</head>
<body>
<cd-hello></cd-hello>
<div cd-hello></div>
<div class="cd-hello"></div>
</body>
</html>
js:
/*20.4 指令 */
var appModule = angular.module('dirAppModule', []);
appModule.run(function($templateCache){
$templateCache.put('hello.html','<h3>hi,code_bunny</h3>')
});
appModule.directive('cdHello',function(){
return {
restrict:'EAC',
templateUrl:'hello.html',
replace:true
}
});
说明: 通过angular创建的模块,都有一个run方法,接受一个函数作为参数.该函数会被执行.
$templateCache是angular内置的一个服务,它的put方法用于存放模板.它接受两个参数,第一个参数为模板的名字,也就是templateUrl的值,这里就是hello.html,第二个参数就是html字符串,也就是模板的内容.
这种方法常用于模板内容是通过$http异步获取的.然后将模板放入$templateCache中以便后面使用.
完整代码:https://github.com/OOP-Code-Bunny/angular/tree/master/OREILLY/20.2%20%E6%8C%87%E4%BB%A4
https://github.com/OOP-Code-Bunny/angular/blob/master/OREILLY/20.3%20%E6%8C%87%E4%BB%A4.html
https://github.com/OOP-Code-Bunny/angular/blob/master/OREILLY/20.4%20%E6%8C%87%E4%BB%A4.html
https://github.com/OOP-Code-Bunny/angular/blob/master/OREILLY/script.js
angular学习笔记(三十)-指令(3)-templateUrl的更多相关文章
- angular学习笔记(三十)-指令(10)-require和controller
本篇介绍指令的最后两个属性,require和controller 当一个指令需要和父元素指令进行通信的时候,它们就会用到这两个属性,什么意思还是要看栗子: html: <outer‐direct ...
- angular学习笔记(三十)-指令(7)-compile和link(2)
继续上一篇:angular学习笔记(三十)-指令(7)-compile和link(1) 上一篇讲了compile函数的基本概念,接下来详细讲解compile和link的执行顺序. 看一段三个指令嵌套的 ...
- angular学习笔记(三十)-指令(6)-transclude()方法(又称linker()方法)-模拟ng-repeat指令
在angular学习笔记(三十)-指令(4)-transclude文章的末尾提到了,如果在指令中需要反复使用被嵌套的那一坨,需要使用transclude()方法. 在angular学习笔记(三十)-指 ...
- angular学习笔记(三十)-指令(5)-link
这篇主要介绍angular指令中的link属性: link:function(scope,iEle,iAttrs,ctrl,linker){ .... } link属性值为一个函数,这个函数有五个参数 ...
- angular学习笔记(三十)-指令(2)-restrice,replace,template
本篇主要讲解指令中的 restrict属性, replace属性, template属性 这三个属性 一. restrict: 字符串.定义指令在视图中的使用方式,一共有四种使用方式: 1. 元素: ...
- angular学习笔记(三十)-指令(7)-compile和link(1)
这篇主要讲解指令中的compile,以及它和link的微妙的关系. link函数在之前已经讲过了,而compile函数,它和link函数是不能共存的,如果定义了compile属性又定义link属性,那 ...
- angular学习笔记(三十)-指令(1)-概述
之前在 angular学习笔记(十九)-指令修改dom 里面已经简单的提到了angular中的指令,现在来详细的介绍 '指令' 一.指令的创建: dirAppModule.directive('dir ...
- angular学习笔记(三十)-指令(8)-scope
本篇讲解指令的scope属性: scope属性值可以有三种: 一.scope:false 默认值,这种情况下,指令的作用域就是指令元素当前所在的作用域. 二.scope:true 创建一个继承了父作用 ...
- angular学习笔记(三十)-指令(7)-compile和link(3)
本篇接着上一篇来讲解当指令中带有template(templateUrl)时,compile和link的执行顺序: 把上一个例子的代码再进行一些修改: 1.将level-two指令改成具有templa ...
随机推荐
- Qt中的QTableView 中的列放入Widget
QTableView是Qt中Model View理念的框架,View只展现数据,所以通过互交修改编辑数据,需要用到委托这个概念Delegate. 所以基本思路是继承QItemDelegate这个类,然 ...
- mysql find_in_set
select * from IpResourceInfo a where find_in_set(a.id,(SELECT group_concat(CAST(resourcesid AS char) ...
- ios中通过过RGB绘制图片
+ (UIImage *) ImageWithColor: (UIColor *) color frame:(CGRect)aFrame { aFrame = CGRectMake(, , aFram ...
- 时间选择器(js,css,html)
忘了从哪下载的了, 整理电脑, 做个记录, 效果图: 下载链接: 链接: https://pan.baidu.com/s/1qfYkcfn8dtLFg0oniB2GHA 提取码: 2amm
- 详解PV、UV、VV、IP及其关系与计算
一.什么是PV? PV即Page View,网站浏览量,指页面浏览的次数,用以衡量网站用户访问的网页数量.用户每次打开一个页面便记录1次PV,多次打开同一页面则浏览量累计.一般来说,PV与来访者的数量 ...
- VBS调用OUTLOOK发送邮件,windows计划任务定时拉起VBS调用OUTLOOK发送邮件
OUTLOOK有延迟传递功能,可延迟传递的发送邮件在功能设计时(mircosoft的support帮助页的解释)就是邮件发送时的时间而不是邮件发送成功后的时间.比如早上10点发一封11点后的延迟传递邮 ...
- HSSFWorkbook 与 XSSFWorkbook
刚开始使用new HSSFWorkbook(new FileInputStream(excelFile))来读取Workbook,对Excel2003以前(包括2003)的版本没有问题,但读取Exce ...
- CListCtrl自适应宽度
原文链接: http://blog.csdn.net/benny5609/article/details/1967084 void CListCtrlExDlg::AdjustColumnWidth( ...
- JetBrains PyCharm专业版激活
PyCharm最新2018激活码 激活时选择License server 填入 http://idea.imsxm.com 然后点击Active即可 PS:在线激活有一个过期时间,这个时间一过就必须再 ...
- C# 对文件与文件夹的操作包括删除、移动与复制
在.Net中,对文件(File)和文件夹(Folder)的操作可以使用File类和Directory类,也可以使用FileInfo类和DirectoryInfo类.文件夹(Folder)是只在Wind ...