angularJs編寫多指令的情況

來源:互聯網
上載者:User

標籤:

本執行個體主要展示controller和link參數的使用.以及多個指令同時作用的情況.

 1 <!DOCTYPE html> 2 <html ng-app="myModule"> 3 <head lang="en"> 4     <meta charset="UTF-8"> 5     <title></title> 6 </head> 7 <body> 8 <div> 9     <div>10         <superman strength>動感超人---力量</superman>11     </div>12     <div>13         <superman strength speed>動感超人---力量+速度</superman>14     </div>15     <div>16         <superman strength speed light>動感超人---力量+速度+發光</superman>17     </div>18 </div>19 20 </body>21 <script type="text/javascript" src="../js/angular1.4.3.js"></script>22 <script type="text/javascript">23 var myModule=angular.module("myModule",[]);24     myModule.directive("superman", function () {25         return{26             scope:{},27             restrict:"AE",28             controller:function($scope){29                 $scope.abilities=[];30                 this.addStrength= function () {31                     $scope.abilities.push("strength");32                 };33                 this.addSpeed= function () {34                     $scope.abilities.push("speed");35                 };36                 this.addLight= function () {37                     $scope.abilities.push(‘light‘);38                 };39             },40             link: function (scope,element,attrs) {41                 element.bind("mouseenter", function () {42                     console.log(scope.abilities);43                 })44             }45         }46     });47     myModule.directive("strength", function () {48         return{49             require:"^superman",50             link: function (scope, element, attrs, supermanCtrl) {51                 supermanCtrl.addStrength();52             }53         }54     });55     myModule.directive("speed", function () {56         return{57           require:"^superman",58             link: function (scope, element, attrs, supermanCtrl) {59                 supermanCtrl.addSpeed();60             }61         };62     });63     myModule.directive("light", function () {64         return{65             require:"^superman",66             link: function (scope,element,attrs,supermanCtrl) {67                 supermanCtrl.addLight();68             }69         }70     })71 </script>72 </html>

angularJs編寫多指令的情況

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.