简体   繁体   English

是否使用:var app = angular.module…还是简单地:angular.module(

[英]Whether to use: var app = angular.module… or simply: angular.module(

I'm new to Angular and am working through various tutorials (Codecademy, thinkster.io and so on) and have seen two ways of declaring the app container. 我是Angular的新手,正在研究各种教程(Codecademy,thinkster.io等),并且看到了两种声明应用程序容器的方法。 Firstly: 首先:

var app = angular.module('myApp', [])

Or simply like this: 或简单地像这样:

angular.module('myApp', [])

Is one better practice over the other or is it simply a different style without any major effect? 是一种比另一种更好的实践,还是只是一种不同的风格而没有任何重大影响?

There'n no difference in how both the approach works except following 两种方法的工作方式没有区别,但遵循以下方法

var app = angular.module('myApp', []) will add an extra global variable(If you're hyper conscious about global variables), however this will shorten your code, don't have to repeat angular.module('myApp') multiple times. var app = angular.module('myApp', [])将增加一个额外的全局变量(如果你是意识的有关全局变量的),但是这会缩短你的代码,不必重复angular.module('myApp')多次。 You can use app instead of angular.module('myApp').xxx at many times. 您可以angular.module('myApp').xxx使用app而不是angular.module('myApp').xxx

You can chain the methods as follow, instead of adding a variable 您可以按以下方式链接方法,而不是添加变量

angular.module('myApp', [])
    .controller(.....)
    .directive(.....)
    .factory(.....);

angular.module supports a third parameter, config function. angular.module支持第三个参数config函数。 If you chain you can do more than one config, which may be useful if you have a lot of configurations. 如果您链接,则可以执行多个配置,如果您有很多配置,这可能会很有用。

angular.module('myModule', [])
    .config(function(injectables) {
        //config block 1
    })
    .config(function(injectables) {
        //config block 2
    });

Instead of: 代替:

angular.module('myModule', [], function(){
    //only config
});

Also it eliminates risk of creating global variables when creating providers. 同样,它消除了创建提供程序时创建全局变量的风险。

There is no difference, only the style and whatever you prefer, see this 没有区别,只有样式和您喜欢的任何东西,都可以看到

angular.module('myApp', [])
  .controller("xCtrl", ['$scope', function($scope){
   }])
   .controller("yCtrl", ['$scope', function($scope){
   }])
  .controller("zCtrl", ['$scope', function($scope){
   }]);

In this case if you put the ; 在这种情况下,如果您输入; after y controll, z controller will not work. y控制后,z控制器将无法工作。 Means all will be treated as a single function. 意味着所有将被视为一个函数。 On the other way: 另一方面:

var app = angular.module('myApp', []);

app.controller("xCtrl", ['$scope', function($scope){
}]);

app.controller("yCtrl", ['$scope', function($scope){
}]);

app.controller("zCtrl", ['$scope', function($scope){
}]);

In it every function is independent. 其中每个功能都是独立的。

The only reason for setting a global variable referring to your module, is if you want to separate it in to multiple files. 设置引用模块的全局变量的唯一原因是,如果要将其分成多个文件。

for example: 例如:

main.js: main.js:

var app = angular.module('myApp', [])
.controller('controller1',.....)
.controller('controller2',.....);

directives.js directives.js

app
.directive('directive1',.....)
.directive('directive2',.....);

although,you should know it is not so good to relay on global variables ( app is global in this example), because they could get overridden by other librarys, and there are other ways to refer to your module in other files, like in this example: 虽然,您应该知道中继全局变量不是很好(在此示例中app是全局的),因为它们可能被其他库覆盖,并且还有其他方法可以在其他文件中引用您的模块,例如:例:

main.js: main.js:

angular.module('myApp', [])
.controller('controller1',.....)
.controller('controller2',.....);

directives.js directives.js

angular.module('myApp')
.directive('directive1',.....)
.directive('directive2',.....);

and if it is a big and complex angular application, you might even want to separate it to multiple modules: 如果它是一个大型且复杂的角度应用程序,则您甚至可能希望将其分为多个模块:

main.js: main.js:

angular.module('myApp', ['myApp.directives'])
.controller('controller1',.....)
.controller('controller2',.....);

directives.js directives.js

angular.module('myApp.directives', [])
.directive('directive1',.....)
.directive('directive2',.....);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM