33

ディレクティブ定義オブジェクトを使用する次のコード (Widget Uno 内) の機能上の違いは何ですか ( .. と呼ばれていると思います)...

angular.module("app").
    directive("widgetUno", ["$http", function ($http) {
        return {
                // A whole bunch of crap going on here
            },
            templateUrl: "widgetUno.html"
        };
    }]);

...Widget Dos のこのコードは?

angular.module("app").directive('widgetDos', function($http) {
    return function(scope, element, attrs) {
        // A whole bunch of crap going on here
    };
});

Widget Uno のようなディレクティブを Widget Dos に変換しようとしていますが、templateUrl はどこで参照すればよいですか? これは Widget Dos で可能ですか?

4

3 に答える 3

44

ディレクティブ内の関数のみを返すことlinkは、完全な定義内の関数の省略形です。

関数以外のもの ( など)を指定する場合は、長い方法で記述する必要があります。linktemplateUrl

angular.module("app").
    directive("widgetUno", ["$http", function ($http) {
        return {
          link: function(scope, element, attrs) {
             // A whole bunch of crap going on here
          },
          templateUrl: "widgetUno.html"
        };
    }]);

この違いは実際にここに文書化されています - http://docs.angularjs.org/guide/directive

于 2013-08-20T20:42:00.690 に答える
9

関数を返すものは、実際には次のショートカットです。

angular.module("app").directive('widgetDos', function($http) {
    return {
        link: function(scope, element, attrs) {
            //...
        };
    }
});

ディレクティブがテンプレート、コントローラーなどを必要としない場合に使用します。それ以外は、これら 2 つの呼び出し方法に機能上の違いはまったくありません。

于 2013-08-20T20:42:01.557 に答える