gpt4 book ai didi

angularjs - 返回的函数在指令声明中做什么

转载 作者:行者123 更新时间:2023-12-03 00:00:36 25 4
gpt4 key购买 nike

我正在学习 AngularJS 中的指令创建。

在官方文档中,它说:

Best Practice: Prefer using the definition object over returning a function.

但它从未给出返回函数的示例。它总是给出返回定义对象的示例。

问题:当您返回函数而不是定义对象时,指令会做什么?

最佳答案

是的,directive documentation没有提到任何有关返回函数的内容。

但是,这个$compile documention确实说:

Comprehensive Directive API

There are many different options for a directive.

The difference resides in the return value of the factory function. You can either return a "Directive Definition Object" (see below) that defines the directive properties, or just the postLink function (all other properties will have the default values).

下面有一个示例(请参阅底部的评论)。

var myModule = angular.module(...);

myModule.directive('directiveName', function factory(injectables) {
var directiveDefinitionObject = {
link: function postLink(scope, iElement, iAttrs) { ... }
};
return directiveDefinitionObject;
// or
// return function postLink(scope, iElement, iAttrs) { ... }
});

关于angularjs - 返回的函数在指令声明中做什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25313927/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com