国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

詳解angularJS自定義指令間的相互交互

2019-11-19 16:09:30
字體:
來源:轉載
供稿:網友

AngularJS 自定義指令

transclude:當元素標簽需要嵌套時使用,與ng-transclude配合使用。默認值為false不能使用嵌套,true為可以使用嵌套。在哪個標簽上使用ng-transclude就在哪個標簽內進行嵌套。

代碼示例:(將hello、hi標簽進行替換同時span標簽嵌套div內)

<script type="text/javascript">  var m = angular.module('myApp',[]);  m.directive('hello',function(){    return{      restrict:'E',      replace:true,      transclude:true,      template:'<div>hello angular<h1 ng-transclude></h1></div>'    };  });  m.directive('hi',function(){    return{      restrict:'E',      replace:true,      template:'<span>hi angular</span>'    };  });  m.controller('Aaa',['$scope',function($scope){    $scope.name='hello';  }]);  </script><body ng-controller="Aaa">  <hello>    <hi></hi>  </hello></body>

頁面結果展示:

這里寫圖片描述

在自定義指令當中controller與link的區別:

link是指DOM操作,操作也是針對當前標簽

controller是多調用性的數據共享,指令與指令間進行交互時也可以設置一些方法數據,在其他標簽中也可以調用

require:從外部引入數據,參數為被引入的指令,被引入的指令需要在引入指令的身上。

》^:是指被引入的指令是引入指令的父級

》?:兼容錯誤

代碼示例:

  <script type="text/javascript">  var m = angular.module('myApp',[]);  m.directive('hello',function(){    return{      restrict:'E',      replace:true,      transclude:true,      controller:function($scope){        //$scope.name='miaov';只能在該標簽中使用        this.name = 'miaov';//可以在其他標簽中調用      },      template:'<div>hello angular<h1 ng-transclude></h1></div>'    };  });  m.directive('hi',function(){    return{      restrict:'E',      replace:true,      require:'?^hello',//從外部引入指令,參數為被引入的標簽      link:function($scope,element,attr,reController){        console.log(reController.name);      },      template:'<span>hi angular</span>'    };  });  m.controller('Aaa',['$scope',function($scope){    $scope.name='hello';  }]);  </script><body ng-controller="Aaa">  <hello>    <hi></hi>  </hello></body>

頁面結果展示:

這里寫圖片描述

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 蒙阴县| 渝北区| 高青县| 临西县| 工布江达县| 南皮县| 平和县| 广丰县| 叙永县| 滕州市| 武乡县| 华阴市| 汶上县| 衡南县| 诸城市| 扎鲁特旗| 宝坻区| 化州市| 厦门市| 隆德县| 平舆县| 崇信县| 巴南区| 丹巴县| 东丽区| 镶黄旗| 上杭县| 平邑县| 富顺县| 靖安县| 利辛县| 宜宾市| 辉县市| 塔城市| 兴义市| 华阴市| 双城市| 荆门市| 新竹市| 大兴区| 祁连县|