【问题标题】:Angular - define modules in multiple filesAngular - 在多个文件中定义模块
【发布时间】:2015-08-13 20:42:18
【问题描述】:

我在 app.js 中有以下内容

config = angular.module('config', [])
.constant('Constants', {
        Car: 'BMW', 
        Phone: 'G4'
});

services = angular.module('services', ['config']);
controllers = angular.module('controllers', ['config', 'services']);
app = angular.module('myApp', ['config', 'controllers']);

我想将“config”模块定义移动到一个单独的文件中,因为我希望它会变得更大。

是否可以将以下部分移动到单独的文件中:

config = angular.module('config', [])
.constant('Constants', {
        Car: 'BMW', 
        Phone: 'G4'
});

【问题讨论】:

  • 你不能改变常量值......
  • 可以有很多文件..不清楚确切的问题是什么或你在问什么。然而,应该将模块分组为功能而不是组件
  • 为了清楚起见,我已经编辑了问题。
  • Is it possible ...您不应该先尝试一下吗?如果你做了什么问题出现了?
  • @charlietfl 因为我们不能在 javascript 中“包含”其他文件。

标签: javascript angularjs coffeescript


【解决方案1】:

这样做没有问题。

您只需要确保配置模块将在应用模块之前加载

【讨论】:

    【解决方案2】:

    您为 Angular 应用选择了非常糟糕的结构。请查看此网址https://scotch.io/tutorials/angularjs-best-practices-directory-structure

    关于你的问题。而不是使用缓存变量,只需做下一件事:

    file0: angular.module('config',[]);
    file1: angular.module('config').constant('Constants', {Car: 'BMW', Phone: 'G4'});
    file2: angular.module('config').constant('Constants2', {Car: 'BMW', Phone: 'G4'});
    

    【讨论】:

    • 我没有提到我的目录结构。它完全符合最佳实践。
    • 我们可以从声明所有内容的方式看到您的结构,vlad 完全正确 imo。他提供的链接也很好。
    猜你喜欢
    • 1970-01-01
    • 2016-12-24
    • 2017-02-20
    • 2019-12-11
    • 2012-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多