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 ...
随机推荐
- 转:典型开源3D引擎分类比较
常见的3D引擎有:Unreal.Quake.Lithtech.OGRE.Nebula.Irrlicht.Truevision3D... 其中开源免费的有:OGRE.irrlicht.fly3d.Neo ...
- 安装 directx sdk 出现 S1023 解决
造成原因: directx sdk 在安装10.0.30319 of the Visual C++ Redistributable的时候发现有一个新版本的 Microsoft Visual C++ 2 ...
- oracle收购Mysql后,Mysql的安装配置方法
自从Oracle收购MySQL后,略微发生了一些小小的变化,原来mysql安装完成后默认是没有密码的,但是新版的mysql安装完成后oracle提供了一个free password放着/root/.m ...
- VS2010编译Boost 1.56
(1)首先下载源代码:http://softlayer-dal.dl.sourceforge.net/project/boost/boost/1.56.0/boost_1_56_0.zip 解压到某个 ...
- 自定义 iPhone 铃声
1.iPhone 铃声格式 iPhone 的来电铃声时长限制为 40 秒,短信铃声时长限制为 25 秒,且 iOS5 及以上的系统才支持 m4r 格式的短信铃声. 2.自定义 iPhone 铃声 1) ...
- Javscript调用iframe框架页面中函数的方法
Javscript调用iframe框架页面中函数的方法,可以实现iframe之间传值或修改值了, 访问iframe里面的函数: window.frames['CallCenter_iframe'].h ...
- Access数据库中日期时间类型的时间段查询
例: select ID,预设点,备注 from 预设点派车预警 where ( 到达时间>=#2013-01-01 12:12:12# and 到达时间<=#2016-01-24 2 ...
- C++11 中值得关注的几大变化(详解)
源文章来自前C++标准委员会的 Danny Kalev 的 The Biggest Changes in C++11 (and Why You Should Care),赖勇浩做了一个中文翻译在这里. ...
- App开发准备
一. Android开发 二. IOS开发 1. 准备苹果电脑 Mac pro 一般比较贵,很少人或公司使用 替代的产品为 iMac 或 Mac mini 中配8G内存版 2. 准备苹果开发者账户,才 ...
- AndroidUI设计 之 图片浏览器
图片浏览器效果图 : 源码下载地址 : -- CSDN : http://download.csdn.net/detail/han1202012/6875083 -- GitHub : https:/ ...