简体   繁体   English

AngularJS模块

[英]AngularJS Module

I am new in AngularJS.I am learning AngularJS. 我是AngularJS的新手,正在学习AngularJS。 I found below syntax. 我发现下面的语法。

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

Here is my question is What does it mean by [ ] this square bracket ?? 这是我的问题是[]这个方括号是什么意思?

What are functionalities/responsibilities of myApp and ngRoute ?? myAppngRoute的功能/职责是什么? What are they doing here ?? 他们在这里做什么?

Which options are available to use like ngRoute ?? 可以使用哪些选项,例如ngRoute

I searched a lot in Google. 我在Google搜索了很多东西。 I got several sample code. 我有几个示例代码。 But could not get any explanation regarding all these things. 但是无法获得关于所有这些事情的任何解释。

Thnanks 谢谢

As per the angular module developer guide found here: https://docs.angularjs.org/guide/module 根据在此处找到的angular module开发人员指南: https : //docs.angularjs.org/guide/module

<div ng-app="myApp">
    <div>
        {{ 'World' | greet }}
    </div>
</div>


var myAppModule = angular.module('myApp', []);
  • The reference to myApp module in <div ng-app="myApp"> . <div ng-app="myApp">对myApp模块的引用。 This is what bootstraps the app using your module. 这就是使用您的模块引导应用程序的内容。
  • The empty array in angular.module('myApp', []) . angular.module('myApp', [])的空数组。 This array is the list of modules myApp depends on. 此数组是myApp依赖的模块的列表。

I suggest you read the official documentation as well: https://docs.angularjs.org/api/ng/function/angular.module 我建议您也阅读官方文档: https : //docs.angularjs.org/api/ng/function/angular.module

Basically [ ] means the module list, your module depends on. 基本上[]表示模块列表,您的模块取决于该列表。

Suppose you write a angular module myApp that depends on ngRoute that is an another angular module. 假设您编写了一个角度模块myApp ,该模块依赖于另一个角度模块ngRoute

The benefits of that you can inject lots of third party angular module that works on different different area. 这样的好处是您可以注入许多在不同区域工作的第三方角度模块。 So you dont have to reinvent the wheel. 因此,您不必重新发明轮子。 By injecting ngRoute you can easily get the functionality of routing in your app. 通过注入ngRoute,您可以轻松地在应用程序中获得路由功能。

I think the description I write helps you to understand clearly 我认为我写的描述可以帮助您清楚地理解

In javascript, you can define a array like this: 在javascript中,您可以定义如下数组:

var arr = [];

the [] here is the same as the [] around 'ngRoute', that means the second parameter of angular.module() method is a array. []这里是一样的[]围绕“ngRoute”,这意味着第二参数angular.module()方法是一个数组。

you can define a module like this as well: 您也可以定义如下模块:

var app = angular.module('awesomeApp', ['ngRoute', 'ngAnimate', 'ngXXX']);

the first param 'awesomeApp' is the name of your module, the second param [ngRoute', 'ngAnimate', 'ngXXX'] is the dependencies of your module. 第一个参数'awesomeApp'是模块的名称,第二个参数[ngRoute', 'ngAnimate', 'ngXXX']是模块的依赖项。

Here the dependency will provide some interfaces or features or functions or any stuff that will help you to make your module work as your expectation. 在这里,依赖项将提供一些接口,功能或特性,或任何有助于您使模块按预期工作的东西。

What you pass as the second arguments is the list of your dependencies. 作为第二个参数传递的是依赖性列表。

In a basic app, is would be just an empty array. 在基本应用中,只是一个空数组。

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

Basically it could be anything, from the 3rd party plugins that you download to other plugins that you yourself wrote to use. 基本上可以是任何东西,从您下载的第三方插件到您自己编写使用的其他插件。 You can find plenty plugins here: http://ngmodules.org/ 您可以在此处找到大量插件: http : //ngmodules.org/

I'd suggest you to read the angular official documents . 我建议你阅读有角度的官方文件

This syntax defines a module. 此语法定义一个模块。

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

myApp this is the name of the app, this is just a string. myApp这是应用程序的名称,这只是一个字符串。

[ngRoute] within the square brackets are the modules you'd like to inject (the notion of dependency injection). 方括号内的[ngRoute]是您要注入的模块(依赖项注入的概念)。 Some common ones you might have seen or will see include ui.bootstrap , restangular , ui.select etc. 您可能已经看到或将会看到的一些常见的东西包括ui.bootstraprestangularui.select等。

Two things worth mentioning: 值得一提的两件事:

  1. Please be careful to not mix it up with the module referencing syntax (without the square brackets): 请注意不要将其与引用语法的模块混淆(没有方括号):

Module definition 模块定义

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

Module referencing 模块引用

angular.module('myApp');
  1. Usually, it's better practice to simply write the module definition out instead of assigning it to a variable, as in 通常,更好的做法是简单地写出模块定义,而不是将其分配给变量,如

Module definition 模块定义

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

instead of 代替

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

You can have a look at Papa John's style guide for more information. 您可以查看Papa John的风格指南以获取更多信息。

https://github.com/johnpapa/angular-styleguide https://github.com/johnpapa/angular-styleguide

Hope this helps. 希望这可以帮助。 :) :)

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

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