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

首頁(yè) > 編程 > JavaScript > 正文

Angular的自定義指令以及實(shí)例

2019-11-19 18:15:41
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

前面的文章介紹了很多angular自帶的指令,下面我們看看如何使用directive自定義指令。

先看一個(gè)例子:

<body> <div my-hello></div></body><script type="text/javascript">var m1 = angular.module('myApp',[]);m1.directive('myHello',function(){ return { restrict : 'A', replace : true, template : '<div>hello angular</div>' };});</script>

1:我們定義了一個(gè)my-hello的指令。

2:使用directive完善這個(gè)指令,返回一個(gè)對(duì)象。有三個(gè)值:

   a) :restrict共四個(gè)值:E:標(biāo)簽指令,C:class指令,M:注釋指令,A:屬性指令

    如何使用 ?

   b):replace是否替換(M注釋必須為true才能解析)看圖:

    true:

    false:

     c):template內(nèi)容,除此之外還有templateUrl,指定一個(gè)html模板文件。

下面再舉個(gè)例子:

 <div ng-controller="Aaa"> <div my-tab my-id="div1" my-name="name" my-fn="show(num)" class="J-tab"></div> <div my-tab my-id="div2" my-name="name" my-fn="show(num)" class="J-tab"></div></div><script type="text/javascript">var m1 = angular.module('myApp',[]);m1.controller('Aaa',['$scope',function($scope){ $scope.name = 'xiecg'; $scope.age = 18; $scope.show = function(num){ console.log(num); };}]);m1.directive('myTab',function(){ return { restrict : 'ECMA', replace : true, //替換的方式插入內(nèi)容//綁定策略 scope : {  myId : '@', //解析普通字符串  myName : '=', //解析數(shù)據(jù)  myFn : '&' //函數(shù) }, controller : ['$scope',function($scope){  //共享數(shù)據(jù)存放在這里  $scope.name = 'this is a xiecg'; }], template : '<div id="{{myId}}">/   <input type="button" value="1" class="active" ng-click="myFn({num:456})">/   <input type="button" value="2">/   <input type="button" value="3">/   <div style="display:block;">{{myName}}</div>/   <div>2222</div>/   <div>3333</div>/  </div>' };});</script>

1:scope默認(rèn)是false,為true表示獨(dú)立作用域。

2:scope給予一個(gè)對(duì)象時(shí),表示執(zhí)行綁定策略,在template上調(diào)用這些數(shù)據(jù)。

  a):我們?cè)贒OM元素上my-id,我們使用@符號(hào),表示解析普通字符串,說(shuō)白了就是你寫(xiě)什麼就是什麼。

  b):使用=符號(hào),表示解析數(shù)據(jù)。

  c):使用&符號(hào),表示這綁定一個(gè)函數(shù)。

3:controller,表示綁定指令內(nèi)部使用的數(shù)據(jù)。

好,下面來(lái)繼續(xù)完善這個(gè)tab切換的例子!

完整代碼:

<!DOCTYPE HTML><html ng-app="myApp"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Tab選項(xiàng)卡實(shí)例</title><style type="text/css"> .J-tab .active{background-color:#03A9F4;} .J-tab div{display:none;}</style><script type="text/javascript" src="js/jquery-1.11.1.js"></script><script type="text/javascript" src="js/angular.min.js"></script></head><body><div ng-controller="Aaa"> <my-tab my-id="div1" my-data="sports" class="J-tab"></my-tab> <my-tab my-id="div2" my-data="time" class="J-tab"></my-tab></div><script type="text/javascript">var m1 = angular.module('myApp',[]);m1.controller('Aaa',['$scope',function($scope){ $scope.sports = [ {title : '籃球',content : '111111111'}, {title : '足球',content : '222222222'}, {title : '排球',content : '333333333'} ]; $scope.time = [ {title : '上午',content : '444444444'}, {title : '中午',content : '555555555'} ];}]);m1.directive('myTab',function(){ return { restrict : 'E', replace : true, scope : {  myId : '@',  myData : '=' }, controller : ['$scope',function($scope){  $scope.name = 'this is a xiecg'; }], template : '<div id="{{myId}}">/   <input ng-repeat="data in myData" type="button" ng-value="data.title" ng-class="{active:$first}">/   <div ng-repeat="data in myData" ng-style="{display:$first?/'block/':/'none/'}">{{data.content}}</div>/  </div>', link : function(scope,element,attr){  element.on('click','input',function(){  var self = $(this) , i = self.index();  self.addClass('active').siblings('input').removeClass('active');  self.siblings('div').eq(i).show().siblings('div').hide();  }); } };});</script></body></html>

link屬性,表示當(dāng)directive被angular編譯后,執(zhí)行該方法。這個(gè)方法接受三個(gè)參數(shù),

a):scope表示controller下面的數(shù)據(jù)。

b):element表示當(dāng)前的DOM元素。

c):attr表示這個(gè)DOM元素上的自定義屬性。

補(bǔ)充:

在實(shí)際的開(kāi)發(fā)過(guò)程中我們往往需要嵌套各種組件和指令。下面來(lái)介紹directive中的transclude和require。

<!DOCTYPE HTML><html ng-app="myApp"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>自定義指令間的互相交互</title><script type="text/javascript" src="js/angular.min.js"></script></head><body><div> <hello> <hi></hi> </hello></div><script type="text/javascript">var m1 = angular.module('myApp',[]);m1.directive('hello',function(){ return { restrict : 'E', replace : true, transclude : true, //允許自定義指令的嵌套,通過(guò)ng-transclude指定嵌套的范圍 controller : function($scope){  $scope.name = 'xiecg';  this.name = 'xiecg'; //使用this共享給其他指令 }, template : '<div>hello angular <h1 ng-transclude></h1></div>' };});m1.directive('hi',function(){ return { restrict : 'E', replace : true, require : '^hello',//hello指令屬性hi指令的父級(jí),需要用^符號(hào)指定。如果無(wú)法指定,使用?容錯(cuò)處理。 template : '<span>hi angular {{name}}</span>', link : function(scope,element,attr,reController){  console.log(reController); //得到父級(jí)hello指令中共享出來(lái)的數(shù)據(jù) } };});</script></body></html>

以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持武林網(wǎng)!

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 县级市| 灯塔市| 虹口区| 礼泉县| 桂林市| 泸州市| 南澳县| 望城县| 丁青县| 锦屏县| 永嘉县| 井陉县| 彭阳县| 庆云县| 兰溪市| 措美县| 绵阳市| 镇坪县| 涡阳县| 新乡县| 晴隆县| 靖江市| 商南县| 石渠县| 静宁县| 道孚县| 万盛区| 缙云县| 塔河县| 辽阳市| 浠水县| 唐海县| 合川市| 平安县| 蕲春县| 紫金县| 嘉黎县| 迭部县| 曲松县| 津市市| 湛江市|