本篇介绍指令的最后两个属性,require和controller

当一个指令需要和父元素指令进行通信的时候,它们就会用到这两个属性,什么意思还是要看栗子:

html:

<outer‐directive>
<inner‐directive></inner‐directive>
</outer‐directive>

这里有两个指令,一个outer-directive指令元素,它里面又有一个inner-directive指令元素.

js:

app.directive('outerDirective',function(){
return {
scope: {},
restrict: 'AE',
controller: function($scope,$compile,$http){
this.addChild = function(nestedDirective){ //this指代controller
console.log('Got the message from nested directive' + nestedDirective.message);
}
}
}
});
app.directive('innerDirective',function(){
return {
scope: {},
restrict: 'AE',
require: '^?outerDirective',
link: function(scope,elem,attrs,ctrl){
//第四个参数是你require的controller实例
scope.message = 'Hi, Parent directive';
ctrl.addChild(scope);
}
};
});

在父元素指令里定义controller方法:

function($scope,$compile,$http){
this.addChild = function(nestedDirective){ //this指代controller
console.log('Got the message from nested directive' + nestedDirective.message);
}
}

controller方法是一个构造函数,可以给它添加对象的方法(什么是对象的方法看这里:mvc-javascript-web-application / 01.MVC和类 / 1.创建类库 / ex1.3(对象的方法).html).

在子元素指令里定义require属性:require: '^?outerDirective',表示向外层寻找outerDirective指令,直到找到为止.注意:向外层寻找,也包括了自己本身,也就是说,自己可以寻找到自己身上的其它指令.

'?'的作用是如果找不到也不会报错.

定义了require属性后,link函数的第四个参数ctrl就是require到的指令的controller方法的实例.所以ctrl就有了addChild方法了.

下面看一个难一点的栗子:

angular学习笔记(三十)-指令(9)-一个简单的指令示例这个栗子是单个子菜单的展开收起,下面使用require和controller,来扩展这个应用,改成多个菜单,要求点击某个菜单的时候展开此菜单,而其余菜单都收起:

如下过程:

html:

<!DOCTYPE html>
<html ng-app="accordionModule">
<head>
<title>20.10 指令-accordion</title>
<meta charset="utf-8">
<link href="../bootstrap.css" rel="stylesheet">
<script src="../angular.min.js"></script>
<script type="text/ng-template" id="vertical.html">
<div class="btn-group-vertical" ng-transclude>
</div>
</script>
<script type="text/ng-template" id="text.html">
<div class="btn-group">
<button class="btn btn-default dropdown-toggle" type="button" ng-click="toggle()">{{title}}<span class="caret"></span></button>
<ul class="dropdown-menu" ng-show="ifShow" ng-transclude></ul>
</div>
</script>
<script src="script.js"></script>
<style type="text/css">
body{
padding:40px
}
</style>
</head>
<body>
<div ng-controller="accordionCtrl">
<accordion>
<expander ng-repeat="list in lists" my-title="{{list.title}}">
<li ng-repeat="content in list.contents">
<a href="">{{content}}</a>
</li>
</expander>
</accordion>
</div>
</body>
</html>

js:

/*20.10 指令*/
var accordionModule = angular.module('accordionModule',[]);
accordionModule.controller('accordionCtrl',function($scope){
$scope.lists = [
{title:'标题1',contents:['bunny1','cat1','dog1']},
{title:'标题2',contents:['bunny2','cat2','dog2']},
{title:'标题3',contents:['bunny3','cat3','dog3']},
{title:'标题4',contents:['bunny4','cat4','dog4']},
{title:'标题5',contents:['bunny5','cat5','dog5']},
{title:'标题6',contents:['bunny6','cat6','dog6']},
{title:'标题7',contents:['bunny7','cat7','dog7']},
{title:'标题8',contents:['bunny8','cat8','dog8']}
]
});
accordionModule.directive('accordion',function(){
return {
restrict:'EA',
replace:true,
templateUrl:'vertical.html',
transclude:true,
controller:function(){
this.expanders = [];
this.closeAll = function(scope){
angular.forEach(this.expanders,function(expander){
if(scope!=expander)
expander.ifShow = false;
})
};
this.addExpander = function(scope){
this.expanders.push(scope)
}
}
}
});
accordionModule.directive('expander',function(){
return {
restrict:'EA',
replace:true,
templateUrl:'text.html',
transclude:true,
scope:{title:'@myTitle'},
require:'^?accordion',
link:function(scope,ele,attrs,ctrl){
scope.ifShow = false;
ctrl.addExpander(scope);
scope.toggle = function(){
ctrl.closeAll(scope);
scope.ifShow = !scope.ifShow;
}
}
}
});

下面来解释这个应用:

1. accordion指令的controller的实例也就是link函数的第四个参数ctrl

2. 将expander指令进行ng-repeat,渲染出多个子菜单.

3. 由于这些子菜单是互相有关联而非独立存在的,所以将他们放在一个accordion指令中.

4. 绑定数据模型都和单独的expander一样,唯一不同的是:

1) link函数中调用ctrl的addExpander方法,将每个expander的独立scope压入ctrl的expanders数组中.

2) toggle()方法,它调用了accordion指令的controller的实例的方法.收起除了当前点击项以外其余的菜单.

5. 注意ctrl是唯一的ctrl,而不是每次都实例化出一个新的实例.虽然有多个expander,但是它的ctrl指向的是同一个实例,所以expanders数组是唯一的.

6. 这里expanders数组里存放是每个expander的scope,为什么是scope而不是element呢? 因为closeAll方法需要修改指令独立作用域下的ifShow的,所以这里数组里存放scope比较好写.

完整代码: https://github.com/OOP-Code-Bunny/angular/blob/master/OREILLY/20.10%20%E6%8C%87%E4%BB%A4.html

https://github.com/OOP-Code-Bunny/angular/blob/master/OREILLY/script.js

angular学习笔记(三十)-指令(10)-require和controller的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  9. angular学习笔记(三十)-指令(4)-transclude

    本篇主要介绍指令的transclude属性: transclude的值有三个: 1.transclude:false(默认值) 不启用transclude功能. 2.transclude:true 启 ...

随机推荐

  1. MySQL的binlog操作

    1. MySQL的binlog有三种模式: statement, row and mixed, 从5.1开始支持row, 默认是row模式 2. 设置参数 # 要配置在mysqld下 [mysqld] ...

  2. 自己动手做——简易下载工具 SimpleDownloader

    一.说明 二.开发 三.源代码 源代码:https://github.com/FrankFan/SimpleDownloader 可执行文件下载地址

  3. [翻译]01-ASP.NET MVC 3介绍

    前言 -------------------------- 最近,公司新架构使用asp.net mvc5,一直都是看书学习ASP.NET MVC的,书本毕竟是别人翻译过来的,所以里面可能某些地方翻译有 ...

  4. null的数据类型

    Oracle的NULL代表的含义是不确定,那么不确定的东西也会有确定的数据类型吗?或者换个说法,NULL在Oracle中的默认数据类型是什么,下面就来探讨这个问题. 首先公布答案,NULL的默认类型是 ...

  5. [Spring学习笔记 7 ] Spring中的数据库支持 RowMapper,JdbcDaoSupport 和 事务处理Transaction

    1.Spring中的数据库支持 把具有相同功能的代码模板抽取到一个工具类中.2.关于jdbc template的应用 jdbcTemplate模板操作类,把访问jdbc的模板抽取到template中, ...

  6. Linux下找不到动态链接库;

    目前,在做的一个程序,编译完后,运行发现报错说找不到自己编译生成的动态库文件,很尴尬;;;果断查资料解决,捎带复制一篇写的比较完善的文章, 地址:http://www.cnblogs.com/wies ...

  7. 关于Ubantu下使用cshell的问题解决

    在一个新创建的目录下使用cshell,直接在/etc/passwd 下对应的用户后 bash改为csh; 之后运行发现报错,后来查找发现/bin下没有csh执行脚本,之后安装csh;; 参考了http ...

  8. Magento模型与ORM基础

    参考网址:http://www.ruiwant.com/magento-for-dev-part-5-magento-models-and-orm-basics.html

  9. 字符串匹配算法-kmp算法

    一原理: 部分转自:http://www.ruanyifeng.com/blog/2013/05/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm.html 字 ...

  10. php 将秒数转换为时间(年、天、小时、分、秒)

    $t=1637544; $d=Sec2Time($t); $d为  0年18天 22小时52分24秒 //将秒数转换为时间(年.天.小时.分.秒) function Sec2Time($time){ ...