本篇介绍指令的最后两个属性,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. PHP执行insert语句报错“Data too long for column”解决办法

    PHP执行mysql 插入语句, insert语句在Navicat for mysql(或任意的mysql管理工具) 中可以正确执行,但是用mysql_query()函数执行却报错. 错误 : “Da ...

  2. 转 一台电脑安装多个tomcat

    只要改这一个就可以了.port 改成8081即可.<Connector port="8081" protocol="HTTP/1.1" connectio ...

  3. 浅析iOS中的触摸事件

    一.什么是响应者对象? 在 iOS中不是任何对象都能处理事件,只有继承了UIResponder的对象才能接收并处理事件.我们称之为“响应者对象”.UIApplication.UIViewControl ...

  4. 运维自动化之Cobbler系统安装使用详解[good]

    一.简介 Cobbler是一个快速网络安装linux的服务,而且在经过调整也可以支持网络安装windows.该工具使用python开发,小巧轻便(才15k行python代码),使用简单的命令即可完成P ...

  5. ubuntu远程桌面连接windows系统

    现在用ubuntu系统,公司买了个windows的服务器,需要给配置一套环境,来回跑很麻烦,就想windows下可以的远程桌面,Linux应该也有. 现在自己的ubuntu13.10,无法进入桌面的“ ...

  6. Linux 操作MySQL常用命令行(转)

    注意:MySQL中每个命令后都要以分号:结尾. 1.显示数据库 mysql> show databases; +----------+ | Database | +----------+ | m ...

  7. Rplidar学习(二)—— SDK库文件学习

    SDK头文件介绍 1.头文件简介: rplidar.h //一般情况下开发的项目中仅需要引入该头文件即可使用 RPLIDAR SDK 的所有功能. rplidar_driver.h //定义了 SDK ...

  8. android 当ListView滚动时自动调用 onCheckedChanged 导致CheckBox 状态不停变化 的解决办法

    今天在做一个含有CheckBox 的ListView时,发现当初始化CheckBox的状态后, 滚动ListView,其中CheckBox 的选中状态不停的发生变化.最后发现原因是 ListView滚 ...

  9. SQL中获取排序分组后数据的脚本

    废话不多说了,先上一段代码,如下所示: select distinct   b.OrgID,b.CompanyID,b.AreaID,b.CustChannelID,b.CustID,b.SaleTo ...

  10. Smarty标签 for运算

    $select="SELECT {$col_name} FROM woke_order where deleted=0 ".$where.$orderby ; // 取出数组 $r ...