What is a Module?
You can think of a module as a container for the different parts of your app – controllers, services, filters, directives, etc.
Why?
Most applications have a main method that instantiates and wires together the different parts of the application.
Angular apps don't have a main method. Instead modules declaratively specify how an application should be bootstrapped. There are several advantages to this approach:
- The declarative process is easier to understand.
- You can package code as reusable modules.
- The modules can be loaded in any order (or even in parallel) because modules delay execution.
- Unit tests only have to load relevant modules, which keeps them fast.
- End-to-end tests can use modules to override configuration.
什么是Module
- 大部分的应用都有一个主方法(main)用来实例化,组织,启动应用。
- AngularJS应用没有主方法,而是用模块来声明应用应该如何启动。
- 模块应许通过声明的方式来描述应用中的依赖关系,以及如何进行组装和启动
Angular模块
- 模块是组织业务的一个框框,在一个模块当中定义多个服务器。当引入了一个模块的时候,就可以使用整个模块提供的一种或者多种服务了。
- AngularJS本身的一个默认模块叫做ng,它提供了 $http, $scope等服务。
- 服务只是模块听过的多种机制中的一种,其他的指令(directive),过滤器(filter),及其他配置信息。
- 也可以在已有的模块中新定义一个服务,也可以先新定义一个模块,然后在新模块中定义新服务。
- 服务是需要显示的声明依赖(引入)关系的,让ng自动的注入。
Module优点
- 启动过程是声名式的,更容易懂。
- 在单元测试是不需要加载全部模块的,因此这种方式有助于写单元测试。
- 可以在特定情况的测试中增加额外的模块,这些模块能更改配置,能帮助进行端对端的测试。
- 第三方代码可以作为可复用的module打包到Angular中。
- 模块可以以任何先后或者并行的顺序加载(因为模块的执行本身是延迟的)。 </