【问题标题】:In Angular , Can we have common App Module or can we have individual Module?在 Angular 中,我们可以有通用的 App 模块还是可以有单独的模块?
【发布时间】:2015-03-19 18:45:34
【问题描述】:

我和我的同事讨论过使用 Angular js 设置我们的应用程序。

我是说我们可以创建一个全局通用模块应用程序(例如:employeeApp),它将被整个应用程序中使用的所有模块使用。因此,如果必须发生任何常见的注入,那么我们将它们注入到 Common Module App 中,我们不需要注入到单个 Module 中。

但他的目的不是创建全局通用模块应用程序。因为 1. 将被javascript工具抱怨的全局变量(例如:jshint)。 2.想让每个模块都独立。如果在特定模块中发生任何问题,它不会影响其他模块。

您如何看待这种方法。哪一个比另一个更好。 ?

【问题讨论】:

  • 不需要全局变量。如果您愿意,我可以提供一个示例,但我认为我不理解您的问题。
  • 为什么我们不需要全局变量?它将避免在另一个模块中重复注入..

标签: angularjs angularjs-module


【解决方案1】:

模块的目的是增加可测试性

考虑到这一点,更多、更小的模块会更好,因为每个模块都可以更好地进行单元测试。

【讨论】:

  • 我还要补充一点,可重用性同样重要。
【解决方案2】:

这是我在没有全局变量的情况下设置我的角度代码的方法。

// app.js
(function(angular) { 'use strict';

  var zaysoApp = angular.module('zaysoApp', 
    ['ngResource','ngRoute','zaysoApp.personModule']);

})(angular);

// person.js
(function(angular) { 'use strict';

  var personModule = angular.module('zaysoApp.personModule', []);

  personModule.factory('personFactory', [ function()
  {
    return { create: function(data) { return new Person(data); }};
  }]);
})(angular);

所以主应用程序模块只需要知道每个单独模块的名称。

我不太明白你的问题,但我知道不需要全局变量。

【讨论】:

    猜你喜欢
    • 2020-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-28
    • 2016-04-06
    • 1970-01-01
    相关资源
    最近更新 更多