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 ...
随机推荐
- Window查看系统激活状态
Window小技巧 #快捷键 Win+G --录像 psr --问题记录器 #查看系统激活信息 WIN键+R调出运行框,在运行框中cmd winver 回车后就能看到当前系统的版本 slmgr.vbs ...
- js template实现方法
<script type="text/html" id="template"> <li class="list-item" ...
- OSS设置CORS规则以后还是报No 'Access-Control-Allow-Origin'解决方法
OSS设置CORS规则以后还是报No 'Access-Control-Allow-Origin'解决方法 在OSS控制台设置了CORS规则以后,通过JS程序去调用的时候报No 'Access-Cont ...
- Redis五大数据类型常用命令脑图
- oracle安装后tnsnames.ora内容
# tnsnames.ora Network Configuration File: D:\Develop\oracle11g\product\11.2.0\dbhome_1\network\admi ...
- Sql Server 在数据库中所有表所有栏位 找出匹配某个值的脚本(转)
转自: http://blog.csdn.net/chenghaibing2008/article/details/11891419 (下面代码稍有修改,将要查找的内容直接作为参数传人,并且使用=而不 ...
- iOS 10 之 网络权限带来的坑
症状 iOS 10 之后,陆陆续续地有用户联系我们,说新机第一次安装.第一次启动的时候,app 首屏一片空白,完全没数据.kill 掉重新打开就好了. 一开始以为是用户网络情况不好,但随着越来越多的用 ...
- VI 基本可视模式
可视模式让你可以选择文件的一部分内容,以便作比如删除,复制等工作. 进入可视模式 v 用v命令进入可视模式.当光标移动时,就能看到有一些文本被高亮显示了,它们就是被选中的内容. 三种可视模式 v 一个 ...
- Elasticstack 5.1.2 集群日志系统部署及实践
Elasticstack 5.1.2 集群日志系统部署及实践 一.ELK Stack简介 ELK Stack 是Elasticsearch.Logstash.Kibana三个开源软件的组合,在实时数据 ...
- Redis学习之路(006)- Redis学习手册(Hashes数据类型)
一.概述: 我们可以将Redis中的Hashes类型看成具有String Key和String Value的map容器.所以该类型非常适合于存储值对象的信息.如Username.Password和Ag ...