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

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

AngularJS中directive指令使用之事件綁定與指令交互用法示例

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

本文實(shí)例講述了AngularJS中directive指令使用之事件綁定與指令交互用法。分享給大家供大家參考,具體如下:

AngularJS中模板的使用,事件綁定以及指令與指令之間的交互

<!doctype html><html ng-app="myapp">  <head>    <meta charset="utf-8"/>  </head>  <body ng-controller="ShieldController">    <div>      <who></who>    </div>    <div>      <button you-btn></button>    </div>    <theshield reigns>343</theshield>    <theshield reigns>fdhg</theshield>    <theshield rollins>hhh</theshield>    <theshield ambros>kkk</theshield>  </body>  <script src="./js/angular.min.js"></script>  <script>    var app = angular.module('myapp',[]);    /*=======================1. 模板的使用 ========================*/    app.directive('who',function(){      return {        restrict:"E",       //元素element 的意思        link:function(scope,element,attrs){          console.log(element);          element[0].innerHTML = 'sdfhkj'; //這個(gè)優(yōu)先級(jí)別最高        },        //templateUrl:"param.html", //這個(gè)不顯示 優(yōu)先級(jí)別最低        template:"<h1>jkdhf</h1>" //這個(gè)顯示 優(yōu)先級(jí)別其次      };    });    /*=======================2. 事件的綁定 ========================*/    app.directive('youBtn',function(){      return {        restrict:"A", //attribute 屬性的意思        link:function(scope,element,attrs){          console.log(element);          element[0].innerHTML = 'my btn';          //事件綁定          element.bind('mouseenter',function(){            element[0].innerHTML = 'your btn';          });          element.bind('mouseleave',function(){            element[0].innerHTML = 'her btn';          });        }      };    });    /*=======================3. 元素 屬性 控制器之間的交互========================*/    app.controller('ShieldController',function($scope){      $scope.shieldNames = [];      this.addReigns = function(){        $scope.shieldNames.push("reigns:jjj");      }      this.addRollins = function(){        $scope.shieldNames.push("Rollins:hhh");      }      this.addAmbros = function(){        $scope.shieldNames.push("Ambros:ggg");      }    })    .directive('reigns',function(){     return {       require:"theshield",       link:function(scope,element,attrs,ShieldController){         ShieldController.addReigns();       }     };    })    .directive('rollins',function(){     return {       require:"theshield",       link:function(scope,element,attrs,ShieldController){         ShieldController.addRollins();       }     };    })    .directive('ambros',function(){     return {       require:"theshield",       link:function(scope,element,attrs,ShieldController){         ShieldController.addAmbros();       }     };    })    .directive('theshield',function(){      return {        restrict:"E",        controller:"ShieldController", //指定控制器        scope:{},           //清空該指令處的$scope 值        link:function(scope,element,attrs){          element.bind('mouseenter',function(){ //對(duì)于該指令所對(duì)應(yīng)的元素綁定對(duì)應(yīng)的事件            console.log(scope.shieldNames);          });        }      };    });  </script></html>

希望本文所述對(duì)大家AngularJS程序設(shè)計(jì)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 雷山县| 三门峡市| 淳安县| 巫溪县| 江津市| 临汾市| 兴文县| 泾源县| 鲁甸县| 林芝县| 徐闻县| 郸城县| 冕宁县| 江门市| 巩留县| 杭锦旗| 长春市| 荣成市| 虞城县| 乾安县| 蒲城县| 太谷县| 米易县| 保康县| 秦皇岛市| 周宁县| 扬中市| 晋城| 阳谷县| 师宗县| 海兴县| 永春县| 新安县| 林芝县| 沂南县| 鹰潭市| 苗栗县| 娄底市| 甘德县| 扎囊县| 安化县|