AngularJS - คำสั่งที่กำหนดเอง

คำสั่งที่กำหนดเองถูกใช้ใน AngularJS เพื่อขยายการทำงานของ HTML คำสั่งที่กำหนดเองถูกกำหนดโดยใช้ฟังก์ชัน "คำสั่ง" คำสั่งที่กำหนดเองจะแทนที่องค์ประกอบที่เปิดใช้งาน แอปพลิเคชัน AngularJS ระหว่าง bootstrap ค้นหาองค์ประกอบที่ตรงกันและทำกิจกรรมครั้งเดียวโดยใช้วิธีการคอมไพล์ () ของคำสั่งที่กำหนดเองจากนั้นประมวลผลองค์ประกอบโดยใช้วิธีการ link () ของคำสั่งที่กำหนดเองตามขอบเขตของคำสั่ง AngularJS ให้การสนับสนุนเพื่อสร้างคำสั่งที่กำหนดเองสำหรับองค์ประกอบประเภทต่อไปนี้

  • 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 html

mainApp.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;
});

กำหนดตัวควบคุมเพื่ออัปเดตขอบเขตสำหรับคำสั่ง ที่นี่เรากำลังใช้ค่าแอตทริบิวต์ของชื่อเป็นลูกของขอบเขต

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 = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/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>

เอาต์พุต

เปิดtextAngularJS.htmในเว็บเบราว์เซอร์ ดูผลลัพธ์