Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/427.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在指令定义中返回对象与返回函数之间的区别?_Javascript_Angularjs_Angularjs Directive - Fatal编程技术网

Javascript 在指令定义中返回对象与返回函数之间的区别?

Javascript 在指令定义中返回对象与返回函数之间的区别?,javascript,angularjs,angularjs-directive,Javascript,Angularjs,Angularjs Directive,以下代码(在Widget Uno中)使用指令定义对象(我认为它被称为..?)之间的功能区别是什么 …这段代码在小部件Dos中 angular.module("app").directive('widgetDos', function($http) { return function(scope, element, attrs) { // A whole bunch of crap going on here }; }); 我试图将类似于Widget Uno的指令

以下代码(在Widget Uno中)使用指令定义对象(我认为它被称为..?)之间的功能区别是什么

…这段代码在小部件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呢?这在小部件Dos中可能吗?

它应该是这样工作的:

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

另请参见(长版本)。有一个例子。

仅返回指令中的函数只是完整定义中
链接
函数的简写

如果指定的不是
链接
函数(如
templateUrl
),则需要长时间编写:

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"
        };
    }]);

此差异实际上记录在此处-

返回函数的实际上是以下各项的快捷方式:

angular.module("app").directive('widgetDos', function($http) {
    return {
        link: function(scope, element, attrs) {
            //...
        };
    }
});
在指令不需要模板、控制器等的情况下使用它。除此之外,这两种调用方法在功能上完全没有区别