Directive间的通信

源自大漠的《AngularJS》5个实例详解Directive(指令)机制

这个例子主要的难点在于如何在子Expander里面访问外层Accordion的scope中的数据

注释解读一下

JS代码:

  1. var expModule=angular.module('expanderModule',[])
  2. expModule.directive('accordion', function() {
  3. return {
  4. restrict : 'EA',
  5. replace : true,
  6. //这里没有定义scope,将使用元素上的scope,就是下面ng-controller指定的SomeController的scope
  7. transclude : true,
  8. template : '<div ng-transclude></div>',
  9. controller : function() {
  10. var expanders = [];
  11. this.gotOpened = function(selectedExpander) {
  12. angular.forEach(expanders, function(expander) {
  13. if (selectedExpander != expander) {
  14. expander.showMe = false;
  15. }
  16. });
  17. }
  18. this.addExpander = function(expander) {
  19. expanders.push(expander);
  20. }
  21. }
  22. }
  23. });
  24. expModule.directive('expander', function() {
  25. return {
  26. restrict : 'EA',
  27. replace : true,
  28. transclude : true,
  29. require : '^?accordion', //依赖
  30. scope : {
  31. title : '=expanderTitle'
  32. },
  33. template : '<div>'
  34. + '<div class="title" ng-click="toggle()">{{title}}</div>'
  35. + '<div class="body" ng-show="showMe" ng-transclude></div>'
  36. + '</div>',
  37. link : function(scope, element, attrs, accordionController) {
  38. //注意link的第四个参数
  39. scope.showMe = false;
  40. accordionController.addExpander(scope);//调用外层指令的方法
  41. scope.toggle = function toggle() {
  42. scope.showMe = !scope.showMe;
  43. accordionController.gotOpened(scope);//调用外层指令的方法
  44. }
  45. }
  46. }
  47. });
  48. expModule.controller("SomeController",function($scope) {
  49. $scope.expanders = [{
  50. title : 'Click me to expand',
  51. text : 'Hi there folks, I am the content that was hidden but is now shown.'
  52. }, {
  53. title : 'Click this',
  54. text : 'I am even better text than you have seen previously'
  55. }, {
  56. title : 'Test',
  57. text : 'test'
  58. }];
  59. });

HTML代码:

  1. <html ng-app="expanderModule">
  2. <head>
  3. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  4. <script src="../angular-1.0.3/angular.min.js"></script>
  5. <link rel="stylesheet" type="text/css" href="Accordion.css"/>
  6. </head>
  7. <body ng-controller='SomeController' >
  8. <accordion>
  9. <expander class='expander' ng-repeat='expander in expanders' expander-title='expander.title'>
  10. {{expander.text}}
  11. </expander>
  12. </accordion>
  13. </body>
  14. <script src="Accordion.js"></script>
  15. </html>

CSS代码:

  1. .expander {
  2. border: 1px solid black;
  3. width: 250px;
  4. }
  5. .expander>.title {
  6. background-color: black;
  7. color: white;
  8. padding: .1em .3em;
  9. cursor: pointer;
  10. }
  11. .expander>.body {
  12. padding: .1em .3em;
  13. }

运行效果:

require option

看看官网的原文怎么说

  1. The myPane directive has a require option with value ^^myTabs. When a directive uses this option, $compile will throw an error unless the specified controller is found.
  2. .The ^^ prefix means that this directive searches for the controller on its parents.
  3. .The ^ prefix would make the directive look for the controller on its own element or its parents;
  4. .without any prefix, the directive would look on its own element only.
  5. .The ? prefix, if not found then pass null to the link as the fourth parameter

link函数的参数

Creating a Directive that Manipulates the DOM

link takes a function with the following signature, function link(scope, element, attrs, controller, transcludeFn) { ... }, where:

. scope is an Angular scope object.

. element is the jqLite-wrapped element that this directive matches.

. attrs is a hash object with key-value pairs of normalized attribute names and their corresponding attribute values.

. controller is the directive's required controller instance(s) or its own controller (if any). The exact value depends on the directive's require property.

. transcludeFn is a transclude linking function pre-bound to the correct transclusion scope.

Directive间的通信的更多相关文章

  1. c 进程间的通信

    在上篇讲解了如何创建和调用进程 c 进程和系统调用 这篇文章就专门讲讲进程通信的问题 先来看一段下边的代码,这段代码的作用是根据关键字调用一个Python程序来检索RSS源,然后打开那个URL #in ...

  2. Ucos系统任务间的通信详解

    物联网开发中,ucos系统任务间的通信是指,两个任务之间有数据的交互,具体的一起来看看吧. 1)消息邮箱 我们还是提供两个任务Task1和Task2,假设我们还是解决刚刚的问题,Task1进行按键扫描 ...

  3. Fragment间的通信

    在网上看到的一篇文章,总结的很好 为了重用Fragment的UI组件,创建的每个Fragment都应该是自包含的.有它自己的布局和行为的模块化组件.一旦你定义了这些可重用的Fragment,你就可以把 ...

  4. angularJS中directive与directive 之间的通信

    上一篇讲了directive与controller之间的通信:但是我们directive与directive之间的通信呢? 当我们两个directive嵌套使用的时候怎么保证子directive不会被 ...

  5. c# 进程间的通信实现之一简单字符串收发

       使用Windows API实现两个进程间(含窗体)的通信在Windows下的两个进程之间通信通常有多种实现方式,在.NET中,有如命名管道.消息队列.共享内存等实现方式,这篇文章要讲的是使用Wi ...

  6. Android进程间的通信之AIDL

    Android服务被设计用来执行很多操作,比如说,可以执行运行时间长的耗时操作,比较耗时的网络操作,甚至是在一个单独进程中的永不会结束的操作.实现这些操作之一是通过Android接口定义语言(AIDL ...

  7. Android进程间的通信之Messenger

    Android进程间的通信方式可以通过以下两种方式完成: Android接口定义语言(AIDL) 使用Messenger绑定服务 本文我们将学习使用Messenger绑定服务的方式进行进程间的通信. ...

  8. 第四节:Vue表单标签和组件的基本用法,父子组件间的通信

    vue表单标签和组件的基本用法,父子组件间的通信,直接看例子吧. <!DOCTYPE html> <html> <head> <meta charset=&q ...

  9. iOS开发多线程篇—线程间的通信

    iOS开发多线程篇—线程间的通信 一.简单说明 线程间通信:在1个进程中,线程往往不是孤立存在的,多个线程之间需要经常进行通信 线程间通信的体现 1个线程传递数据给另1个线程 在1个线程中执行完特定任 ...

随机推荐

  1. mysql5.7绿色版安装与配置

    1,找到zip archive包下载,官方地址如下: http://dev.mysql.com/downloads/mysql/ http://dev.mysql.com/downloads/file ...

  2. Rust语言的多线程编程

    我写这篇短文的时候,正值Rust1.0发布不久,严格来说这是一门兼具C语言的执行效率和Java的开发效率的强大语言,它的所有权机制竟然让你无法写出线程不安全的代码,它是一门可以用来写操作系统的系统级语 ...

  3. label 多行显示自适应高度

    //项目中显示 地址:XXXXXXX换行  UILabel *numLable = [[UILabel alloc] initWithFrame:CGRectMake(80, 50, 40, 20)] ...

  4. sass和compass的配置

    http://note.youdao.com/share/?id=4f57187f9695bcaadf80516187d2de0e&type=note 当执行安装sass出错时,可以试试这个命 ...

  5. jsp 中 有没有类似java if else语句

    <c:when test=""></c:when> <c:otherwise></c:otherwise> 有if else的功能 ...

  6. ubuntu下出现的问题-控制台更新源失败

    Ubuntu下控制台输入sudo apt-get update之后出现的问题:E: Could not get lock /var/lib/apt/lists/lock - open (11: Res ...

  7. 【翻译】Express web应用开发 第一章

    本章节是一个对初学者友好的Express介绍.你将学习到Express的基础知识.核心概念和实现一个Express应用的组成部分.现阶段我们不需要做太多的编码,本章节会让你熟悉和习惯Express,为 ...

  8. 【Beta】Daily Scrum Meeting第二次

    1.任务进度 学号 已完成 接下去要做 502 系负责人及所负责专业的表 写出PHP测试的demo:将okHttp的请求放在非UI线程中执行 509 PHP更该用户信息:更新系负责人所负责系:删除任务 ...

  9. [ios]关于用FMDB 操作数据库 删除 tableView 后刷新

    刚了解使用fmdb,从数据库获取数据 绑定到一个可变数组classNameItems //从ClassList表取得数据 FMResultSet *classInfo=[db executeQuery ...

  10. 如何通过倾斜摄影数据手动配置s3c索引文件?

    如何通过倾斜摄影数据手动配置s3c索引文件? 大家知道,倾斜摄影数据最常见的是OSGB格式,并且是由一个一个的Tile分级文件夹构成的Data文件夹.结构一般如下图所示: 那么,如何才能把模型的各个瓦 ...