简体   繁体   English

角度:何时在模块和控制器中使用“ []”

[英]Angular : When to use “[]” in modules and controllers

I'm starting in Angular and i don't understand when to use or not [] when declaring (if it's called like this) modules and controllers. 我从Angular开始,在声明(如果这样称呼)模块和控制器时,我不明白何时使用或不使用[]

So far, i understood that : 到目前为止,我了解到:

angular.module('app.prof', []).config(config); means i declare the module prof 表示我声明模块prof

while angular.module('app.prof').config(config); angular.module('app.prof').config(config); means i call it. 就是我叫它

But i always end up having the [] in the controller and not in the module, and when i have two controllers i don't know what to do anymore. 但是我总是最终在控制器中而不是模块中有[] ,并且当我有两个控制器时,我不知道该做什么了。

I just want to understand Angular :( 我只想了解Angular :(

Edit : adding more details 编辑:添加更多详细信息

In my Angular project, i use the Fuse theme with a thing called Yeoman (if i understood well) so i never write <script> tags anywhere myself. 在我的Angular项目中,我将Fuse主题与一个称为Yeoman的东西一起使用(如果我理解得很好),因此我永远不会在自己的任何地方写<script>标签。

for example, i have this module : 例如,我有这个模块:

    angular
    .module('app.prof')
    .config(config);

and this controller : 和这个控制器:

    angular
    .module('app.prof', [])
    .controller('ProfController', ProfController);

My modules and controllers are always declared this way, but sometimes i get errors like "module is not declared" or "controller is not declared"; 我的模块和控制器总是以这种方式声明的,但是有时我会遇到类似“未声明模块”或“未声明控制器”的错误; making me end up randomly putting brackets in the modules or controllers until it works. 让我最终随机将括号放在模块或控制器中,直到它起作用为止。

EDIT 2 : 编辑2:

here is my main module : 这是我的主要模块:

angular
    .module('fuse', ['app.prof']);

my prof module : 我的教授模块:

    angular
    .module('app.prof')
    .config(config);

and my prof controller : 和我的教授控制器:

    angular
    .module('app.prof', [])
    .controller('ProfController', ProfController);

this way, the whole thing works fine. 这样,整个事情就很好了。 But when i do : 但是当我这样做时:

    angular
    .module('app.prof', [])
    .config(config);

and

    angular
    .module('app.prof')
    .controller('ProfController', ProfController);

i get : 我得到:

错误

The second argument are the dependencies for the module. 第二个参数是模块的依赖关系。 Since you don't have any dependencies at the moment you have an empty array [] . 由于您目前没有任何依赖项,因此您有一个空数组[]

You add the brackets (dependencies) only when you create a new module. 仅在创建新模块时才添加方括号(依赖项)。 Every time when you simply want to use the same module you shouldn't have brackets. 每当您只想使用相同的模块时,都不需要括号。 So in the end every module should be declared only once with the []. 因此,最后每个模块仅应使用[]声明一次。 And every other time should be called without. 并且每隔一个时间都应调用不带。

Also make sure that files and code is imported in the correct order. 还要确保以正确的顺序导入文件和代码。 Otherwise the module is not yet defined when you try to add controllers to it. 否则,当您尝试向其添加控制器时,尚未定义该模块。

Small example. 小例子。 App module with multiple controllers. 具有多个控制器的应用模块。

angular.module('app', []);

angular.module('app').controller('aCtrl', [ function () {} ]);
angular.module('app').controller('bCtrl', [ function () {} ]);

// ...

Long story short. 长话短说。 If you declare a module, you need the brackets. 如果声明模块,则需要括号。 If you want to use the module you don't add them. 如果要使用模块,则不要添加它们。


Edit: For your edit, once again, you need to make sure that you define and use everything in correct order. 编辑:再次进行编辑时,需要确保以正确的顺序定义和使用所有内容。

Following depends on app.prof which means that app.prof declaration needs to be before this row: 以下内容取决于app.prof ,这意味着app.prof声明需要在此行之前:

angular.module('fuse', ['app.prof']);

This already suggests that module app.prof is declared and you try to add config to it. 这已经表明已声明模块app.prof ,并且您尝试向其添加配置。 When app.prof is not declared before you get a similar error as you displayed: 如果未声明app.prof,然后显示类似的错误:

angular.module('app.prof')
    .config(config);

This declares the module app.prof and adds a controller to that module: 这将声明模块app.prof ,并向该模块添加一个控制器:

angular.module('app.prof', [])
    .controller('ProfController', ProfController);

This declares the app.prof module ands immediately adds some config to it: 这将声明app.prof模块并立即app.prof添加一些配置:

angular.module('app.prof', [])
    .config(config);

This adds a controller to an existing module called app.prof : 这会将控制器添加到名为app.prof的现有模块中:

angular.module('app.prof')
    .controller('ProfController', ProfController);

I think you get the idea... 我想你应该已经明白了...

As for a solution you need to declare these in an order similar to this... Doesn't matter if everything is in one file or you load them in file by file but the module declaration order is important. 对于解决方案,您需要以类似于此的顺序声明它们...一切都在一个文件中还是将它们逐个文件加载都没关系,但是模块声明顺序很重要。

angular.module('app.prof', [])
    .config(config);

angular.module('app.prof')
    .controller('ProfController', ProfController);

angular.module('fuse', ['app.prof']);

Controller declaration can probably happen later but anyway you need to pay attention to the ordering. 控制器声明可能会在以后发生,但是无论如何您都需要注意顺序。 As you said that fuse is the main module you need to declare the app.prof before. 正如您所说的, fuse是主要模块,您需要在之前声明app.prof

In angular the "[]" is meant for injecting the external(angular) dependencies to you module and get access of it. 用[[]]表示的是将外部(angular)依赖项注入到模块中并对其进行访问。 refer the below example 请参考以下示例

angular.module('app.proof',['myservice']).controller(function(bootstrap,myservice){
 myservice.callmethod();});

Hope This helps! 希望这可以帮助!

So when you create a module, you can add other modules as dependencies to it that you're planning to use: 因此,在创建模块时,可以将其他模块作为依赖项添加到计划使用的模块中:

var app = angular.module('myModule', ['Restangular', 'UserService', 'otherModule']);

Then, when you create a controller, you can make services, factories, etc. available to the controller. 然后,在创建控制器时,可以使控制器可以使用服务,工厂等。

app.controller('MyNewController', ['$scope', '$http', 'restangular', function($scope, $http, restangular) {
    // Your code here
    $scope.name = "Daniel";
});

You can declare a Module like this: 您可以这样声明一个模块:

 angular.module('myModule', []);

Similarly, you can declare a Controller like this: 同样,您可以这样声明一个控制器:

 angular.module('myModule')
    .controller('MyController', []);

When you declare the controller, you're not using the empty array notation in the module, because you're defining a controller on an already existing module. 在声明控制器时,您不会在模块中使用空数组符号,因为您是在现有模块上定义控制器。

The empty array ( [] ) that you pass into the module or controller is a placeholder for dependencies you want to inject into your module or controller. 传递给模块或控制器的空数组( [] )是要注入模块或控制器的依赖项的占位符。 See: AngularJS Depedency Injection . 请参阅: AngularJS掺杂注入

Every AngularJS project has a 'top-level' or root module. 每个AngularJS项目都有一个“顶层”或根模块。 This is the module that bootstraps your application together when you use it in accordance with an ng-app directive. 当您根据ng-app指令使用应用程序时,该模块会将您的应用程序引导到一起。

In your top-level module(s), you declare your dependencies to the other modules in your application. 在顶层模块中,您声明对应用程序中其他模块的依赖性。 For example: 例如:

angular.module('root', ['childModule1', 'childModule2', 'childModule3']);

In your controllers, you use dependency injection to inject services, functions, or AngularJS' built-in libraries (such as $http, $sce, or $q) For example: 在控制器中,您可以使用依赖项注入来注入服务,函数或AngularJS的内置库(例如$ http,$ sce或$ q),例如:

angular.module('root')
    .controller('RootController', ['$window', '$location', 'myRootService']);

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

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