詳解angularJS自定義指令間的相互交互
AngularJS 自定義指令
transclude:當(dāng)元素標(biāo)簽需要嵌套時(shí)使用,與ng-transclude配合使用。默認(rèn)值為false不能使用嵌套,true為可以使用嵌套。在哪個(gè)標(biāo)簽上使用ng-transclude就在哪個(gè)標(biāo)簽內(nèi)進(jìn)行嵌套。
代碼示例:(將hello、hi標(biāo)簽進(jìn)行替換同時(shí)span標(biāo)簽嵌套div內(nèi))
<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>
頁(yè)面結(jié)果展示:
在自定義指令當(dāng)中controller與link的區(qū)別:
link是指DOM操作,操作也是針對(duì)當(dāng)前標(biāo)簽
controller是多調(diào)用性的數(shù)據(jù)共享,指令與指令間進(jìn)行交互時(shí)也可以設(shè)置一些方法數(shù)據(jù),在其他標(biāo)簽中也可以調(diào)用
require:從外部引入數(shù)據(jù),參數(shù)為被引入的指令,被引入的指令需要在引入指令的身上。
》^:是指被引入的指令是引入指令的父級(jí)
》?:兼容錯(cuò)誤
代碼示例:
<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';只能在該標(biāo)簽中使用 this.name = 'miaov';//可以在其他標(biāo)簽中調(diào)用 }, template:'<div>hello angular<h1 ng-transclude></h1></div>' }; }); m.directive('hi',function(){ return{ restrict:'E', replace:true, require:'?^hello',//從外部引入指令,參數(shù)為被引入的標(biāo)簽 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>
頁(yè)面結(jié)果展示:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 深入講解AngularJS中的自定義指令的使用
- AngularJS創(chuàng)建自定義指令的方法詳解
- AngularJS優(yōu)雅的自定義指令
- AngularJS使用自定義指令替代ng-repeat的方法
- AngularJS 自定義指令詳解及實(shí)例代碼
- AngularJS自定義指令實(shí)現(xiàn)面包屑功能完整實(shí)例
- AngularJS實(shí)現(xiàn)自定義指令與控制器數(shù)據(jù)交互的方法示例
- AngularJS 自定義指令詳解及示例代碼
- AngularJS自定義指令之復(fù)制指令實(shí)現(xiàn)方法
- AngularJS自定義指令詳解(有分頁(yè)插件代碼)
- AngularJS實(shí)現(xiàn)自定義指令及指令配置項(xiàng)的方法
相關(guān)文章
深入理解Angularjs中$http.post與$.post
本篇文章主要介紹了深入理解Angularjs中$http.post與$.post ,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05AngularJS學(xué)習(xí)筆記之TodoMVC的分析
這篇文章主要介紹了AngularJS學(xué)習(xí)筆記之TodoMVC的分析的相關(guān)資料,需要的朋友可以參考下2015-02-02AngularJS操作鍵值對(duì)象類似java的hashmap(填坑小結(jié))
我們知道java的hashmap中使用最多的是put(...),get(...)以及remove()方法,那么在angularJS中如何創(chuàng)造(使用)這樣一個(gè)對(duì)象呢?今天小編通過本文給大家分享下,感興趣的朋友一起學(xué)習(xí)吧2016-11-11AngularJS基礎(chǔ) ng-value 指令簡(jiǎn)單示例
本文主要介紹AngularJS ng-value 指令,這里對(duì)ng-value 的基礎(chǔ)資料做了整理,并附實(shí)例代碼,有需要的小伙伴可以參考下2016-08-08詳解基于angular-cli配置代理解決跨域請(qǐng)求問題
本篇文章主要介紹了詳解基于angular-cli配置代理解決跨域請(qǐng)求問題,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07