简体   繁体   English

我是否需要为与我的角度应用程序相关的每个文件声明一个角度模块var?

[英]Do i have to declare an angular module var for each file related to my angular app?

I want to write directive in directive.js and write controller in controller.js but I must write app = angular.module('app') , and about app's config in every file? 我想写指令在directive.jscontroller.js写控制器,但我必须编写app = angular.module('app')以及有关在每个文件的应用程序的配置? Is there a simple way to write them? 有写它们的简单方法吗?

Do not take the simple answer of "yo angular". 不要简单回答“ yo angular”。 This gives you no understanding of how to setup your own build based on what your application needs. 这使您不了解如何根据应用程序需求来设置自己的构建。

In terms of directory structure i use something like the following: 在目录结构方面,我使用以下内容:

应用程序目录结构

If you have other things to go into your app/ then you could move all your features to a folder ie "modules" 如果您还有其他要进入应用程序的内容,则可以将所有功能移至文件夹,即“模块”

In terms of how i start my module code and the relational controllers / directives etc of that module. 在我如何启动我的模块代码以及该模块的关系控制器/指令等方面。 It would look something like the following blocks: 看起来类似于以下块:

File: app/foo/foo.js 文件:app / foo / foo.js

(function() {

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

})();

File: app/foo/controllers/foo.fooController.js 文件:app / foo / controllers / foo.fooController.js

(function() {

    function fooController() {
        // controller code
    }

    angular.module('foo').controller('fooController', fooController);

})();

File: app/foo/directives/foo.fooDirective.js 文件:app / foo / directives / foo.fooDirective.js

(function() {

    function fooDirective() {
        // directive code
    }

    angular.module('foo').directive('fooDirective', fooDirective);

})();

You don't have to cache the module in the global scope, you could just use the getter syntax of module angular.module('app') in every file/everytime you need it. 您不必在全局范围内缓存模块,只需在每个文件中/每次需要时使用模块 angular.module('app')getter语法 angular.module('app') With this you do not pollute global scope by placing a variable ex:- app in order to be able to use it elsewhere. - :有了这个,你,不要将一个变量前污染全球范围内app ,以便能够在其他地方使用它。

controller.js: controller.js:

//controller def
angular.module('app').controller('myCtrl', MyCtrl);

directive.js: 指令.js:

//directive def
angular.module('app').directive('myDir', MyDir);

Just do not reinitialize the module by doing angular.module('app',[]) elsewhere other than it is intended to be. 只是不要在预期之外的其他地方通过执行angular.module('app',[])重新初始化模块。

I too struggled with this concept. 我也为这个概念而苦恼。 It comes down to this. 归结为这个。

First think of an AngularJS Module as a namespace declaration, then consider the following. 首先将AngularJS Module视为namespace声明,然后考虑以下内容。

Given an application namespace called "foo", you may have several files that make up your application, but you want all of them to just be extending the "foo" namespace. 给定一个名为“ foo”的应用程序名称空间,您可能有几个文件组成了您的应用程序,但您希望所有这些文件都只是扩展“ foo”名称空间。 AngularJS' module function has two prototypes; AngularJS的模块功能有两个原型。 the first takes two parameters, the second just one. 第一个带有两个参数,第二个只有一个。 In your main module file (let's call it the one that loads first) you will use the two parameter version to initialize your application and define what external modules you want Angular to include. 在主模块文件中(我们称其为首先加载的文件),您将使用两个参数版本来初始化应用程序并定义希望Angular包括哪些外部模块。

The following code example shows the usage of the two formats. 下面的代码示例显示了两种格式的用法。 The first one shows how to perform the initial declaration of a modules. 第一个显示了如何执行模块的初始声明。 Note the second parameter, which is an array of strings that identify the modules that your modules are dependent on; 注意第二个参数,它是一个字符串数组,用于标识模块所依赖的模块; even if you do not have dependencies and empty array must be supplied. 即使您没有依赖项,也必须提供空数组。

//main.js
(function(angular,undefined){
    "use strict";

    angular.module("foo", ["ui-router"]);
})(angular);

In the second part of the example you should note that there is no second parameter. 在示例的第二部分,您应注意没有第二个参数。 This basically tells Angular that you have already initialized the module elsewhere, now you are just trying to add to it. 这基本上告诉Angular您已经在其他地方初始化了模块,现在您只是尝试添加到模块中。

//bar-controller.js
(function(angular,undefined){
    "use strict";

    angular.module("foo")
        .controller("bar", ["$scope", "$state", function($scope, $state){
             //Do controller stuff
        });
})(angular);

As to how to organize the files? 至于如何组织文件? That is up to you. 那取决于你。 Whatever makes sense to you and your application. 对您和您的应用程序有意义的一切。 AngularJS really does not care about the structure of your file system, other than all paths to templates are relative to the "main" js file. AngularJS确实不在乎文件系统的结构,除了模板的所有路径都是相对于“主要” js文件的。

As far as i understand, what u need to know is if you have to declare an app variable in every .js file that will contain Angular code... (The title of the question mismatch with the content) 据我了解,您需要知道的是您是否必须在每个将包含Angular代码的.js文件中声明一个应用程序变量...(问题的标题与内容不匹配)

Short answer: NO 简短答案:

Explanation 说明

You can declare an angular app in this way 您可以通过这种方式声明一个角度应用

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

that will let you declare controllers, services, etc.. like this 这样您就可以声明控制器,服务等。

myAppModule.controller('MyCTRL', ['dep1','dep2', function(dep1,dep2){
      //...
 }]);

Or you can declare it as a module without assigning a variable to it 或者您可以将其声明为模块而无需为其分配变量

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

then you'll have to declare controllers, services, etc... like this 那么您必须像这样声明控制器,服务等

angular.module('myAppModule').controller('MyCTRL', ['dep1','dep2', function(dep1,dep2){
      //...
    }]);

Now, to completely answer your question, once you declare your module, you don't have to declare it again, just remember to load the .js file that contains the declaration of the app module before the ones that has de controllers, directives, services, etc. 现在,要完全回答您的问题,一旦声明了模块,就不必再次声明它,只需记住在包含de controller,指令,服务等

Also you should check this out: 'Best' way to declare a module 您还应该检查一下: 声明模块的“最佳”方法

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

相关问题 我是否总是必须在angular2中声明变量才能获得更改? - Do I always have to declare a variable in angular2 to get changes? 我在为 Angular 安装节点模块 depandances 时遇到问题 - I have a problem installing my node module depandances for Angular 如何使用Angular声明特定模块 - How to declare specific module with Angular 是否使用:var app = angular.module…还是简单地:angular.module( - Whether to use: var app = angular.module… or simply: angular.module( 如何在ng-show中放入localStorage var? (Angular JS / Firebase) - How do I put a localStorage var inside my ng-show ? (Angular JS/Firebase) 当我将服务放在不同的文件时,Angular会抛出错误 - Angular throws an error when I have my service at a different file 为什么我的指令模块未包含在我的Angular应用程序模块中? - Why is my directives module not being included in my Angular app module? 如何在Angular应用中重新加载现有的地图对象? - How do I reload an existing map object in my Angular app? 无法将我本地开发的Angular模块导入Angular应用程序 - Unable to import my locally developed Angular module into Angular app 如何装扮我的angular2应用javascript文件 - How do I concattinate my angular2 app javascript files
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM