【问题标题】:AngularJs Inject multiple directives from separate files into one common moduleAngularJs 将不同文件中的多个指令注入到一个通用模块中
【发布时间】:2017-04-04 08:43:58
【问题描述】:

有一个关于在不同模块中引用和注入指令的问题。目标是将位于不同文件中的多个指令注入一个模块,然后将该公共模块注入其他地方。我有多个指令,在单独的文件中定义,例如:

define(['angular'], function (angular) {

angular.module('ngCustomDirective')
    .directive('ngCustomDirective', function () {
       ...
    });
});

在单独的文件中,我有:

define(['angular'], function (angular) {

angular.module('ngCustomDirective2')
    .directive('ngCustomDirective2', function () {
       ...
    });
});

之后该指令在另一个模块(不同的文件)中被引用:

define(['angular','ngCustomDirective', 'ngCustomDirective2'], function (angular, ngCustomDirective, ngCustomDirective2) {

angular.module('directives', [ngCustomDirective, ngCustomDirective2]);

return 'directives';
});

接下来,这个“指令”模块被注入到另一个模块中。上面的代码不起作用。我在这里做错了什么?

【问题讨论】:

    标签: angularjs angularjs-directive angularjs-scope


    【解决方案1】:

    您可以尝试在下面的单引号中注入模块吗?

    angular.module('directives', ['ngCustomDirective', 'ngCustomDirective2']);

    【讨论】:

    • 这不是问题,还有另一个问题。指令应该在没有模块的情况下定义。 AngularJs 中有一种方法可以在没有将模块指定为函数的情况下定义指令。然后我可以毫无问题地从其他模块引用它。在我的代码中,模块的定义存在问题:angular.module('ngCustomDirective') 应该定义为 angular.module('ngCustomDirective', []) 或者现在,我试图避免在指令中定义模块定义。
    【解决方案2】:

    按照此答案的指南进行操作,这正是您需要做的。 open link

    【讨论】:

      猜你喜欢
      • 2018-05-14
      • 2014-08-20
      • 1970-01-01
      • 2014-01-03
      • 1970-01-01
      • 1970-01-01
      • 2017-02-24
      • 1970-01-01
      • 2023-03-29
      相关资源
      最近更新 更多