angularJS中directive與directive 之間的通訊

來源:互聯網
上載者:User

標籤:css   lin   his   module   通訊   har   console   dem   border   

上一篇講了directive與controller之間的通訊;但是我們directive與directive之間的通訊呢?

當我們兩個directive嵌套使用的時候怎麼保證子directive不會被父directive替換覆蓋;及當子directive需要用到父directive中controller某個變數或者方法的時候

怎麼實現兩個directive之間的通訊的。

這裡主要講directive的兩個屬性:

1.transclude

2.require

 

html

 1 <!DOCTYPE html> 2 <html> 3 <head lang="en"> 4     <meta charset="UTF-8"> 5     <title>angular-directive3</title> 6     <script src="js/angularjs1.4/angular-1.4.6.mini.js"></script> 7     <style type="text/css"> 8         #id1 { 9             width: 200px;10             height: 100px;11             border: 1px solid #005599;12         }13         #id2 {14             width: 100px;15             height: 50px;16             margin: 5px;17             border: 1px dotted #cccccc;18         }19     </style>20 </head>21 <body ng-app="demoDrt">22 23 <deep-drt forid="id1">24     <inner-drt forinnerid="id2"></inner-drt>25 </deep-drt>26 27 </body>28 </html>

js

 1 var demoDrt = angular.module(‘demoDrt‘, []); 2     demoDrt.directive(‘deepDrt‘, function() { 3         return { 4             restrict:‘AE‘, 5             replace:true, 6             scope : { 7                 forid : ‘@‘ 8             }, 9             //加上transclude屬性true之後在template中結合ng-transclude指令就不會替換子directive,10             // 如果嵌套有自訂指令而沒加transclude屬性的話子directive會被父directive的template替換掉11             transclude:true,12             //如果有子directive要使用父directive中controller的值的話變數和函數的定義則要使用this來綁定,$scope綁定的話只會在當前範圍生效13             controller:[‘$scope‘, function($scope) {14                 //這個this輸出的其實就是controller對象15                 this.name = ‘angular‘;16                 this.version = ‘1.4.6‘;17             }],18             //template 上結合ng-transclude使用,如果ng-transclude放在template的父級的話,19             // 那麼template裡面的值會被子directive覆蓋,所以我們要用一個dom並加上ng-transclude來在外層包裹子directive。20             template:‘<div id="{{forid}}">deepDrt<div ng-transclude></div></div>‘21         };22     });23 24     demoDrt.directive(‘innerDrt‘, function() {25        return {26            restrict:"AE",27            replace:true,28            //require主要作用是尋找父directive,‘^‘表示向上尋找,後面加上父directive的名字,‘?‘表示如果找不到的話則不會報錯,29            // 一般‘^?‘兩個結合使用當找不到父directive的時候angular不會報錯30            //結合了require 則在子directive的link屬性中加上reController,則可以擷取父directive中controller的變數和方法31            require: ‘^?deepDrt‘,32            scope : {33                forinnerid : ‘@‘34            },35            link : function(scope, element, attr, reController) {36                console.log(scope);37                console.log(attr);38                //reController得到了父controller中綁定的變數和函數39                element.bind(‘click‘, function(e) {40                   console.log(e.target);41                    e.target.innerText = reController.name + ‘-‘ + reController.version;42                });43            },44            template : ‘<div id="{{forinnerid}}">innerDrt</div>‘45        }46     });

directive產生頁面結構

 

通過transclude:true同時在template中結合ng-transclude指令我們能把子directive嵌套在父directive中;而我在注釋中也提到了,如果ng-transclude外沒有一個dom包裹

子directive的話那字directive也是會被父directive的template替換的。

在子directive中require:‘^?deepDrt‘;具體含義在注釋中已經說明。值得注意的是父directive中的controller中定義的變數如果要給子directive使用的話要用this來綁定變數或者方法,this指的就是controller這個對象。子directive中reController得到的就是這個controller對象,從而得到父directive中this定義的變數和屬性。

 

關於angularJS directive之間的通訊就這麼多;不對之處希望大家指正。謝謝!

 

angularJS中directive與directive 之間的通訊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.