这篇主要介绍指令中的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. MySQL表名大小写敏感导致的问题

      最近在项目中遇到一个比较奇怪的小问题.在开发过程中自己测试没有问题,但是提测后,测试的同时在测试一个功能时报错了,日志是: Caused by: com.mysql.jdbc.exceptions ...

  2. Ubuntu16.04下添加打印机FujiXerox CP116w

    今天要打印一份北马的成绩单, 不想重启机器了, 在Ubuntu下尝试添加打印机, 最后成功了, 记录一下 打印机型号是FujiXerox CP116w, 通过WIFI连接的, 在Ubuntu16.04 ...

  3. 转:Ogre TerrainGroup地形赏析

    1.1  参考 http://www.ogre3d.org/tikiwiki/tiki-index.php?page=Ogre+Terrain+System http://www.ogre3d.org ...

  4. awbeci—一个帮助你快速处理日常工作的网址收集网站

    大家好,我是awbeci作者,awbeci网站是一个能够快速处理日常工作的网址收集网站,为什么这样说呢?下面我将为大家介绍这个网站的由来,以及设计它的初衷和如何使用以及对未来的展望和计划,以及bug反 ...

  5. 阿里云k8s服务springboot项目应用升级时出现502错误

    背景 随着小步快跑.快速迭代的开发模式被越来越多的互联网企业认同和采用,应用的变更.升级频率变得越来越频繁.为了应对不同的升级需求,保证升级过程平稳顺利地进行,诞生了一系列的部署发布模式. 停机发布 ...

  6. at com.mysql.jdbc.SQLError.createSQLException

    WARN run, com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector@1de6191 -- APPARENT DEA ...

  7. [Spring学习笔记 3 ] spring 注解详解,完全注解,常用注解

    .xml使用注解 xml 用来定义bean的信息,注解用来配置依赖信息 ) 在配置文件中配置bean )在javaBean中用注解来指定依赖注入 )在配置文件中开启注解扫描 @Resource标签 j ...

  8. spring 2种下载方式 下载地址 download 地址

    spring 在官网只提供 maven 的下载方式,把zip方式的不再提供,两种方法下载: 1.想找回以前版本的spring zip包,如果知道版本号,那么直接在google里输入 ” spring ...

  9. 九款命令行工具助力Linux环境下的数据分析

    对于大多数熟悉了图形工作环境的朋友来说,电子表格工具无疑是第一选项.但命令行工具同样能够更快更高效地解决问题——且只须稍微学习即可上手. 大部分此类工具冻严格局限于Linux,而多数可同样运行在Uni ...

  10. 如何解决 yum安装出现This system is not registered with RHN

    [root@localhost ~]# yum install libtool Loaded plugins: rhnplugin, security This system is not regis ...