这篇主要介绍指令中的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的更多相关文章

  1. angular学习笔记(三十)-指令(10)-require和controller

    本篇介绍指令的最后两个属性,require和controller 当一个指令需要和父元素指令进行通信的时候,它们就会用到这两个属性,什么意思还是要看栗子: html: <outer‐direct ...

  2. angular学习笔记(三十)-指令(7)-compile和link(2)

    继续上一篇:angular学习笔记(三十)-指令(7)-compile和link(1) 上一篇讲了compile函数的基本概念,接下来详细讲解compile和link的执行顺序. 看一段三个指令嵌套的 ...

  3. angular学习笔记(三十)-指令(6)-transclude()方法(又称linker()方法)-模拟ng-repeat指令

    在angular学习笔记(三十)-指令(4)-transclude文章的末尾提到了,如果在指令中需要反复使用被嵌套的那一坨,需要使用transclude()方法. 在angular学习笔记(三十)-指 ...

  4. angular学习笔记(三十)-指令(5)-link

    这篇主要介绍angular指令中的link属性: link:function(scope,iEle,iAttrs,ctrl,linker){ .... } link属性值为一个函数,这个函数有五个参数 ...

  5. angular学习笔记(三十)-指令(2)-restrice,replace,template

    本篇主要讲解指令中的 restrict属性, replace属性, template属性 这三个属性 一. restrict: 字符串.定义指令在视图中的使用方式,一共有四种使用方式: 1. 元素: ...

  6. angular学习笔记(三十)-指令(7)-compile和link(1)

    这篇主要讲解指令中的compile,以及它和link的微妙的关系. link函数在之前已经讲过了,而compile函数,它和link函数是不能共存的,如果定义了compile属性又定义link属性,那 ...

  7. angular学习笔记(三十)-指令(1)-概述

    之前在 angular学习笔记(十九)-指令修改dom 里面已经简单的提到了angular中的指令,现在来详细的介绍 '指令' 一.指令的创建: dirAppModule.directive('dir ...

  8. angular学习笔记(三十)-指令(8)-scope

    本篇讲解指令的scope属性: scope属性值可以有三种: 一.scope:false 默认值,这种情况下,指令的作用域就是指令元素当前所在的作用域. 二.scope:true 创建一个继承了父作用 ...

  9. angular学习笔记(三十)-指令(7)-compile和link(3)

    本篇接着上一篇来讲解当指令中带有template(templateUrl)时,compile和link的执行顺序: 把上一个例子的代码再进行一些修改: 1.将level-two指令改成具有templa ...

随机推荐

  1. Fiddler拦截并修改移动端请求

    bpu url 由于测试电商平台APP,需测试购买,但又限于公司一提到钱,就给种不给力,所以想到使用Fiddler拦截消息,修改一个虚拟商品ID,虚拟商品价格为0.01元,方便以后测试. 1.打开Fi ...

  2. 跟我学Shiro---无状态 Web 应用集成

    无状态 Web 应用集成 在一些环境中,可能需要把 Web 应用做成无状态的,即服务器端无状态,就是说服务器端不会存储像会话这种东西,而是每次请求时带上相应的用户名进行登录.如一些 REST 风格的 ...

  3. 【转】 mysql反引号的使用(防冲突)

    转载地址:http://blog.itechol.com/space.php?uid=33&do=blog&id=6681 1.mysql语句中 反引号 [`]作用: 避免表明.字段名 ...

  4. 前台导出Word文档思路步骤总结(freemarker)

    1. 需求是导出word带表格,表格列数不变,行数由数据库的值决定: 2. 导出最开始想的是直接前端导出,使用了jquery-wordexport插件,导出后,表格边框全没了,无法使用: 3. 采用了 ...

  5. shell脚本npm构建静态文件

    #!/bin/bash cd /data/web source /etc/profile /usr/bin/cnpm i && npm run build cp -r ./dist/* ...

  6. Tensorflow get_variable和Varialbe的区别

    import tensorflow as tf """ tf.get_variable()和Variable有很多不同点 * 它们对重名操作的处理不同 * 它们受name ...

  7. 【总结 】550,535,553 Mail from must equal authorized user— jenkins(hudson) email163邮箱和26邮箱成功配置总结

    Failed to send out e-mail com.sun.mail.smtp.SMTPSendFailedException: 553 Mail from must equal author ...

  8. ubuntu(14.04) 下配置重写

    1.开启rewrite模块,使用命令:a2enmod  rewrite 2.在apache2.conf,配置你网站的目录(是目录而定)

  9. Android程序员必备精品资源

    平时写程序中不断收集到的一些比较常用的东西,分享给大家. 实用工具集锦 Android Lifecycle https://github.com/xxv/android-lifecycle TinyP ...

  10. 【Oracle】Oracle中常用的系统函数

    Oracle SQL 提供了用于执行特定操作的专用函数.这些函数大大增强了 SQL 语言的功能.函数可以接受零个或者多个输入参数,并返回一个输出结果.在Oracle还可以自定义函数,关于更多信息可以查 ...