2016-04-26 6 views
0

私たちは、カスタムディレクティブを定義する方法について読むと、以下の方法で発見しました:anglejsでカスタムディレクティブを定義する方法はいくつありますか?

angular.module("myApp", []) 
    .directive("directiveName", function() { 
     return { 
      // implementation code will go here 
     } 
    }); 

をしかし、最近、私は以下の通りであるカスタムディレクティブを定義するための別の方法を見つけました:

angular.module("exampleApp", []) 
    .directive("directiveName", function() { 
     return function (scope, element, attrs) { 
      // implementation code will go here 
     } 
    }); 

私は、どちらの方法が他の方法よりも速く、より速いかを知ることに興味がありますか? (可能であれば、両者の長所と短所を説明してください)、それ以上カスタムディレクティブを定義する方法はありますか?

+0

[マニュアル]は(https://docs.angularjs.org/guide/directive#creating-directives)は、ベストプラクティスは、機能ではなく、定義オブジェクトを使用することであると言います。 – yarons

答えて

0

ディレクティブで関数を返すことは、完全な定義のリンク関数の略記に過ぎません。あなただけのリンク機能が必要な場合

angular.module("myApp", []). 
    directive("directiveName", function() { 
     return { 
      link: function(scope, element, attrs) { 
       // implementation code will go here 
      }, 
      templateUrl: "template.html", // for example 
     }; 
    }) 
; 

は代わりに、あなたはshortandバージョンを使用することができます:あなたはリンク機能以上のものを指定している場合
だから、あなたはそれを長い道のりを記述する必要があります。

angular.module("myApp", []) 
    .directive("directiveName", function() { 
     return { 
      // implementation code will go here 
     } 
    }) 
; 
関連する問題