AngularJS中directive指令使用之事件綁定與指令交互用法示例
更新時間:2016年11月22日 11:42:22 作者:栁羅風塵
這篇文章主要介紹了AngularJS中directive指令使用之事件綁定與指令交互用法,結合實例形式分析了directive指令在模板的使用,事件的綁定及元素、屬性、控制器之間的交互相關操作技巧,需要的朋友可以參考下
本文實例講述了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'; //這個優(yōu)先級別最高 }, //templateUrl:"param.html", //這個不顯示 優(yōu)先級別最低 template:"<h1>jkdhf</h1>" //這個顯示 優(yōu)先級別其次 }; }); /*=======================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(){ //對于該指令所對應的元素綁定對應的事件 console.log(scope.shieldNames); }); } }; }); </script> </html>
希望本文所述對大家AngularJS程序設計有所幫助。
相關文章
ui-router中使用ocLazyLoad和resolve的具體方法
這篇文章主要介紹了ui-router中使用ocLazyLoad和resolve的具體方法,詳細的介紹了ocLazyLoad和resolve的具體用法,非常具有實用價值,需要的朋友可以參考下2017-10-10Angular通過angular-cli來搭建web前端項目的方法
這篇文章主要介紹了Angular通過angular-cli來搭建web前端項目的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07Angular 通過注入 $location 獲取與修改當前頁面URL的實例
這篇文章主要介紹了Angular 通過注入 $location 獲取與修改當前頁面URL的實例代碼,需要的朋友可以參考下2017-05-05AngularJS框架中的雙向數(shù)據(jù)綁定機制詳解【減少需要重復的開發(fā)代碼量】
這篇文章主要介紹了AngularJS框架中的雙向數(shù)據(jù)綁定機制,結合實例形式分析了AngularJS雙向數(shù)據(jù)綁定機制的原理及實現(xiàn)方法,以及減少需要重復開發(fā)代碼量的優(yōu)勢,需要的朋友可以參考下2017-01-01