2016-12-01 36 views
0

我见过两种不同的方法来组织Angular 1.x中的模块和控制器结构。角组织和模块化 - 控制器结构

第一个是我熟悉的方式,将控制器分隔到自己的文件中并将它们附加到父角度模块。

,创建一个较大的控制器文件来容纳相关控制器并将它们附加到父控制器,然后将控制器作为模块注入到父角度模块中。

哪个更高性能?有区别吗?

App.js

angular 
    .module('mainApp', [ 
     'a bunch of dependencies' 
    ]) 
    .run(['a bunch of dependencies', function(a bunch of dependencies){ 

myControllerA.js

angular 
    .module('mainApp') 
    .controller('myControllerA', ['a bunch of dependencies', 
     function(a bunch of dependencies) { 

myControllerB.js

angular 
    .module('mainApp') 
    .controller('myControllerB', ['a bunch of dependencies', 
     function(a bunch of dependencies) { 

App.js

var app = angular.module('mainApp', ['mainController', 'a bunch of dependencies']); 

app.run(['a bunch of dependencies', function(mainController, a bunch of dependencies) { 

mainController.js

var mainController = angular.module('mainController', [ 
    'a bunch of dependencies' 
]); 

mainController.config(function(a bunch of dependencies) { 
    ... 
}); 

mainController.controller('controllerA', ['a bunch of dependencies', function(a bunch of dependencies) { 
    ... 

mainController.controller('controllerB', ['a bunch of dependencies', function(a bunch of dependencies) { 
    ... 
+1

看看这个问题http://stackoverflow.com/questions/20995605/does-a-large-number-of-modules-hit-performance-of-angularjs –

回答

1

什么已经真正的帮助,我是追随this angular styleguide。这是Angular团队的认可,并且由于结构如何,如果您必须这么做,它会让Angular 2变得不那么痛苦。

如果你不喜欢那样,上面链接中引用的another style guide也是很好的。

使用这些样式指南中的任何一个,它们都推荐将文件分离出来。我不认为这是因为性能与易用性相当,特别是随着应用程序的增长。让一切都在一个文件中将变得难以为继