模块

模块是应用程序各个部分的容器 - 控制器,服务,过滤器,指令等。

为什么使用模块
大多数应用程序都有一个主要方法,可以实例化并将应用程序的不同部分连接在一起。
Angular 应用程序没有主要方法。
但是在 AngularJs 中,声明性过程很容易理解,并且可以将代码打包为可重用模块。
模块可以按任何顺序加载,因为模块会延迟执行。

声明一个模块

var app = angular.module('myApp', []);
// Empty array is list of modules myApp is depends on.
// if there are any required dependancies, 
// then you can add in module, Like ['ngAnimate']

app.controller('myController', function() {

  // write your business logic here
});

模块加载和依赖关系

  1. 配置块: - 在提供者和配置阶段执行。

    angular.module('myModule', []).
    config(function(injectables) {
      // here you can only inject providers in to config blocks.
    });
    
  2. 运行块: - 在创建注入器后执行并用于启动应用程序。

    angular.module('myModule', []).
    run(function(injectables) {
      // here you can only inject instances in to config blocks.
    });