麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > JavaScript > 正文

AngularJS 自定義指令詳解及示例代碼

2019-11-20 09:13:08
字體:
來源:轉載
供稿:網友

自定義指令中使用AngularJS擴展HTML的功能。自定義指令使用的“指令”的功能定義。自定義指令只是替換了它被激活的元素。引導過程中AngularJS應用程序找到了匹配的元素,并做好使用自定義指令compile()方法一次活動再處理使用基于指令的范圍自定義指令link()方法的元素。 AngularJS提供支持,以下列元素的類型來創(chuàng)建自定義指令。

Element directives - 指令遇到時激活一個匹配的元素。

Attribute - - 指令遇到時激活一個匹配的屬性。

CSS - - 指令遇到時激活匹配CSS樣式。

Comment - - 指令遇到時激活匹配的注釋。

了解自定義指令

定義自定義的HTML標簽。

<student name="Mahesh"></student><br/>
<student name="Piyush"></student>

定義自定義指令來處理上面的自定義HTML標簽。

var mainApp = angular.module("mainApp", []);//Create a directive, first parameter is the html element to be attached.	 //We are attaching student html tag. //This directive will be activated as soon as any student element is encountered in htmlmainApp.directive('student', function() { //define the directive object var directive = {}; //restrict = E, signifies that directive is Element directive directive.restrict = 'E'; //template replaces the complete element with its text.  directive.template = "Student: <b>{{student.name}}</b> , Roll No: <b>{{student.rollno}}</b>"; //scope is used to distinguish each student element based on criteria. directive.scope = {  student : "=name" } //compile is called during application initialization. AngularJS calls it once when html page is loaded. directive.compile = function(element, attributes) {  element.css("border", "1px solid #cccccc");	 //linkFunction is linked with each element with scope to get the element specific data.  var linkFunction = function($scope, element, attributes) {   element.html("Student: <b>"+$scope.student.name +"</b> , Roll No: <b>"+$scope.student.rollno+"</b><br/>");   element.css("background-color", "#ff00ff");  }  return linkFunction; } return directive;});

定義控制器以更新范圍為指令。在這里,我們使用name屬性值作為子的作用域。

mainApp.controller('StudentController', function($scope) {  $scope.Mahesh = {};  $scope.Mahesh.name = "Mahesh Parashar";  $scope.Mahesh.rollno = 1;  $scope.Piyush = {};  $scope.Piyush.name = "Piyush Parashar";  $scope.Piyush.rollno = 2;});

例子

<html><head> <title>Angular JS Custom Directives</title></head><body> <h2>AngularJS Sample Application</h2> <div ng-app="mainApp" ng-controller="StudentController">		<student name="Mahesh"></student><br/>		<student name="Piyush"></student> </div> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script> <script>  var mainApp = angular.module("mainApp", []);	   mainApp.directive('student', function() {   var directive = {};   directive.restrict = 'E';   directive.template = "Student: <b>{{student.name}}</b> , Roll No: <b>{{student.rollno}}</b>";      directive.scope = {   student : "=name"   }		    directive.compile = function(element, attributes) {   element.css("border", "1px solid #cccccc");   var linkFunction = function($scope, element, attributes) {    element.html("Student: <b>"+$scope.student.name +"</b> , Roll No: <b>"+$scope.student.rollno+"</b><br/>");    element.css("background-color", "#ff00ff");   }   return linkFunction;   }   return directive;  });	   mainApp.controller('StudentController', function($scope) {   $scope.Mahesh = {};   $scope.Mahesh.name = "Mahesh Parashar";   $scope.Mahesh.rollno = 1;   $scope.Piyush = {};   $scope.Piyush.name = "Piyush Parashar";   $scope.Piyush.rollno = 2;  });   </script></body></html>

結果

在Web瀏覽器中打開textAngularJS.html。看到結果如下:

以上就是對AngularJS的自定義指令資料整理,后續(xù)繼續(xù)補充,謝謝大家對本站的支持!

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 免费放黄网站在线播放 | 日韩在线播放第一页 | 成人区一区二区三区 | 久久精品1区2区 | 一级α片免费看刺激高潮视频 | 久久久久久久久久久久久久国产 | 久久久久久麻豆 | 成人福利软件 | 本站只有精品 | 欧美一级精品 | 中文字幕一二三区芒果 | 国产精品av久久久久久网址 | 久久国产精品久久久久久久久久 | 黄网在线 | 大学生一级毛片在线视频 | 黄色免费av网站 | 色呦呦一区二区三区 | 操碰视频在线观看 | 免费黄色在线观看网站 | 免费观看一级黄色片 | 日韩黄色免费在线观看 | 国产精品一区在线看 | 国产免费美女 | 久久国产秒| 噜噜噜躁狠狠躁狠狠精品视频 | 久久久久久久久亚洲精品 | 鲁人人人鲁人人鲁精品 | 欧美午夜网 | 亚洲日本高清 | 日本精品网 | 草久影视 | 激情黄页| 国产福利视频在线观看 | www.热| 国产色视频一区 | 精品99在线视频 | 国产精品久久久久永久免费 | 日韩黄色片免费看 | 国产在线看一区 | 久久精品男人 | 欧美中文字幕一区二区三区亚洲 |