最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

AngularJS 指令的交互詳解及實例代碼

 更新時間:2016年09月14日 17:32:22   作者:xingoo  
這篇文章主要介紹了AngularJS 指令的交互,這里整理了詳細(xì)的資料及實例代碼,有興趣的小伙伴可以參考下

  背景介紹

  這例子是視頻中的例子,有一個動感超人,有三種能力,力量strength,速度speed,發(fā)光light。

  這三種能力作為三種屬性,定義動感超人作為一個標(biāo)簽,只要添加對應(yīng)的屬性就能擁有該能力。

  為了便于結(jié)果的展示,為標(biāo)簽添加鼠標(biāo)的響應(yīng)事件,當(dāng)鼠標(biāo)移動到對應(yīng)的標(biāo)簽上就會觸發(fā)一個方法,打印出具備的能力。

  程序分析
  html部分的代碼如下:       

 <div>
      <superman>nothing!</superman>
      <superman strength >strength!</superman>
      <superman strength speed >strength speed!</superman>
      <superman strength speed light >strength speed light!</superman>
    </div>

  下面看看如何實現(xiàn),首先依然是創(chuàng)建一個模塊:

var myAppModule = angular.module("myApp",[]);

  在該模塊的基礎(chǔ)上,創(chuàng)建標(biāo)簽superman,與前面類似。

myAppModule.directive("superman",function(){
        return{
          scope:{},
          restrict:'AE',
          transclude:true,
          template:"<div><div ng-transclude></div></div>",
          controller:function($scope){
            $scope.abilities = [];
            this.addStrength = function(){
              $scope.abilities.push("strength");
            };
            this.addSpeed = function(){
              $scope.abilities.push("speed");
            };
            this.addLight = function(){
              $scope.abilities.push("light");
            };
          },
          link:function(scope,element,attr){
            element.bind("mouseenter",function(){
              console.log(scope.abilities);
            });
          }
        }
      });

  這里不同的是,在方法內(nèi)部有一個controller屬性,這個并不是ng-controller這種控制器,而是指令對外開放的一個接口,里面聲明的方法,在外部可以作為公開的方法使用,其他的指令可以通過依賴,使用這些方法。

  接下來再創(chuàng)建三個能力對應(yīng)的指令

    myAppModule.directive("strength",function(){
        return{
          require:'^superman',
          link:function(scope,element,attr,supermanCtrl){
            supermanCtrl.addStrength();
          }
        }
      });
      myAppModule.directive("speed",function(){
        return{
          require:'^superman',
          link:function(scope,element,attr,supermanCtrl){
            supermanCtrl.addSpeed();
          }
        }
      });
      myAppModule.directive("light",function(){
        return{
          require:'^superman',
          link:function(scope,element,attr,supermanCtrl){
            supermanCtrl.addLight();
          }
        }
      });

  三個指令的代碼都差不多,其中require指定了依賴的指令。

  link中多了一個參數(shù)supermanCtrl,這個參數(shù)猜想是superman中的controller,所以命名采用superman+Ctrl的方式。

  【由于不懂內(nèi)部原理,這里僅僅是猜想,但是實驗證明,如果改變這個參數(shù)的名字,會報錯?!?/p>

  聲明了這三個指令,就可以把這三個指令當(dāng)做super的屬性來使用,當(dāng)注明該屬性時,就會觸發(fā)內(nèi)部的link內(nèi)的方法,調(diào)用superman中公開的方法。  

  總結(jié)起來,指令的交互過程:

  1 首先創(chuàng)建一個基本的指令,在controller屬性后,添加對外公開的方法。

  2 創(chuàng)建其他交互的指令,在require屬性后,添加對應(yīng)的指令依賴關(guān)系;在link中調(diào)用公開的方法

  全部程序代碼:

<!doctype html>
<html ng-app="myApp">
  <head>
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     <script src="http://apps.bdimg.com/libs/angular.js/1.2.16/angular.min.js"></script>
  </head>
  <body>

    <div>
      <superman>nothing!</superman>
      <superman strength >strength!</superman>
      <superman strength speed >strength speed!</superman>
      <superman strength speed light >strength speed light!</superman>
    </div>
    <script type="text/javascript">
      var myAppModule = angular.module("myApp",[]);

      myAppModule.directive("superman",function(){
        return{
          scope:{},
          restrict:'AE',
          transclude:true,
          template:"<div><div ng-transclude></div></div>",
          controller:function($scope){
            $scope.abilities = [];
            this.addStrength = function(){
              $scope.abilities.push("strength");
            };
            this.addSpeed = function(){
              $scope.abilities.push("speed");
            };
            this.addLight = function(){
              $scope.abilities.push("light");
            };
          },
          link:function(scope,element,attr){
            element.bind("mouseenter",function(){
              console.log(scope.abilities);
            });
          }
        }
      });
      myAppModule.directive("strength",function(){
        return{
          require:'^superman',
          link:function(scope,element,attr,supermanCtrl){
            supermanCtrl.addStrength();
          }
        }
      });
      myAppModule.directive("speed",function(){
        return{
          require:'^superman',
          link:function(scope,element,attr,supermanCtrl){
            supermanCtrl.addSpeed();
          }
        }
      });
      myAppModule.directive("light",function(){
        return{
          require:'^superman',
          link:function(scope,element,attr,supermanCtrl){
            supermanCtrl.addLight();
          }
        }
      });
    </script>
  </body>
</html>


 

 運(yùn)行結(jié)果:

        以上就是對AngularJS 指令的交互的資料整理,后續(xù)繼續(xù)補(bǔ)充相關(guān)資料,謝謝大家對本站的支持!

相關(guān)文章

  • angular.fromJson與toJson方法用法示例

    angular.fromJson與toJson方法用法示例

    這篇文章主要介紹了angular.fromJson與toJson方法用法,結(jié)合實例形式分析了AngularJS使用fromJson與toJson方法進(jìn)行json格式數(shù)據(jù)的解析與轉(zhuǎn)換相關(guān)操作技巧,需要的朋友可以參考下
    2017-05-05
  • AngularJS HTML編譯器介紹

    AngularJS HTML編譯器介紹

    這篇文章主要介紹了AngularJS HTML編譯器介紹,AngularJS的HTML編譯器能讓瀏覽器識別新的HTML語法。它能讓你將行為關(guān)聯(lián)到HTML元素或者屬性上,甚至能讓你創(chuàng)造具有自定義行為的新元素,需要的朋友可以參考下
    2014-12-12
  • AngularJS自定義指令詳解(有分頁插件代碼)

    AngularJS自定義指令詳解(有分頁插件代碼)

    本篇文章主要介紹了AngularJS自定義指令詳解(有分頁插件代碼),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • Angular2中select用法之設(shè)置默認(rèn)值與事件詳解

    Angular2中select用法之設(shè)置默認(rèn)值與事件詳解

    在Angular.JS中可以使用數(shù)組或?qū)ο髣?chuàng)建一個下拉列表選項。關(guān)于Angular.js中select的基礎(chǔ)相信大家應(yīng)該都已經(jīng)了解了,那么下面這篇文章主要給大家介紹了Angular2中select用法之設(shè)置默認(rèn)值與事件的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-05-05
  • Angular2 http jsonp的實例詳解

    Angular2 http jsonp的實例詳解

    這篇文章主要介紹了Angular2 http jsonp的實例詳解的相關(guān)資料,希望通過本能幫助到大家,需要的朋友可以參考下
    2017-08-08
  • angular2 組件之間通過service互相傳遞的實例

    angular2 組件之間通過service互相傳遞的實例

    今天小編就為大家分享一篇angular2 組件之間通過service互相傳遞的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-09-09
  • Angular7實現(xiàn)拖放Drag?Drop示例詳解

    Angular7實現(xiàn)拖放Drag?Drop示例詳解

    這篇文章主要介紹了Angular7實現(xiàn)拖放Drag?Drop示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-12-12
  • AngularJS獲取json數(shù)據(jù)的方法詳解

    AngularJS獲取json數(shù)據(jù)的方法詳解

    這篇文章主要介紹了AngularJS獲取json數(shù)據(jù)的方法,結(jié)合實例形式詳細(xì)分析了AngularJS獲取json數(shù)據(jù)的詳細(xì)步驟、操作技巧與相關(guān)注意事項,需要的朋友可以參考下
    2017-05-05
  • AngularJs表單驗證實例詳解

    AngularJs表單驗證實例詳解

    這篇文章主要介紹了用AngularJs驗證表單實例詳解的相關(guān)資料,需要的朋友可以參考下
    2016-05-05
  • AngularJS中$watch和$timeout的使用示例

    AngularJS中$watch和$timeout的使用示例

    這篇文章給大家介紹了AngularJS中$watch和$timeout的使用例子,通過示例代碼相信更能讓大家理解,有需要的朋友們下面來一起看看吧。
    2016-09-09

最新評論

红原县| 东明县| 雅江县| 左权县| 龙游县| 商水县| 分宜县| 普洱| 敦化市| 贺州市| 女性| 缙云县| 呼和浩特市| 栖霞市| 临洮县| 大理市| 胶南市| 建昌县| 曲沃县| 左权县| 高尔夫| 碌曲县| 普洱| 江津市| 同心县| 武川县| 琼海市| 库车县| 贵南县| 黎平县| 钦州市| 延津县| 修水县| 赤壁市| 黄冈市| 韩城市| 九江市| 塘沽区| 马山县| 芜湖市| 开化县|