Angularjs-custom-directives

提供:Dev Guides
移動先:案内検索

AngularJS-カスタムディレクティブ

AngularJSでは、HTMLの機能を拡張するためにカスタムディレクティブが使用されます。 カスタムディレクティブは、「ディレクティブ」関数を使用して定義されます。 カスタムディレクティブは、アクティブ化された要素を単に置き換えるだけです。 ブートストラップ中のAngularJSアプリケーションは、一致する要素を見つけ、カスタムディレクティブのcompile()メソッドを使用して1回アクティビティを実行し、ディレクティブのスコープに基づいてカスタムディレクティブのlink()メソッドを使用して要素を処理します。 AngularJSは、次のタイプの要素のカスタムディレクティブの作成をサポートします。

  • 要素ディレクティブ-一致する要素が見つかると、ディレクティブがアクティブになります。
  • 属性-一致する属性が検出されると、ディレクティブがアクティブになります。
  • CSS -一致するCSSスタイルが検出されると、ディレクティブがアクティブになります。
  • コメント-一致するコメントが検出されると、ディレクティブがアクティブになります。

カスタムディレクティブについて

カスタム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) {
         elementl("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 = "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) {
                  elementl("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_を開きます。 結果をご覧ください。