AngularJS高级程序设计读书笔记 -- 模块篇

时间:2022-09-20 15:35:32

一. 模块基础

1. 创建模块

<!DOCTYPE html>
<html ng-app="exampleApp">
<head>
    <title>Test</title>
    <script type="text/javascript">
        var myApp = angular.module("exampleApp", []);
        myApp.controller('dayCtrl', function ($scope) {
            // controller statements will go here
        });
    </script>
</head>
<body>
    <div class="panel" ng-controller="dayCtrl">
        <!-- HTML code here -->
    </div>
</body>
</html>

模块的三种重要角色:

  • 将一个 AngularJS 应用程序 与 HTML 文档中的一部分相关联
  • 起到通往 AngularJS 框架的关键特性的门户作用.
  • 帮助组织一个 AngularJS 应用程序中的代码和组件.

angular.module 方法所接受的参数

名称 描述
name 新模块的名称
requires 该模块所以来的模块集合
config 盖默快的配置, 等效于调用 Module.config 方法.

angular.module 方法返回一个 Module 对象. 该 Module 对象的成员方法如下表:

名称 描述
animation(name, factory) 支持动画特性, 见 第23章
config(callback) 注册一个在模块加载时对该模块进行配置的函数,见 9.4.1
constant(key, value) 定义一个返回一个常量的服务, 见 9.4.1
controller(name, constructor) 创建一个控制器, 见 13 章
directive(name, factory) 创建一个指令, 对标准HTML词汇进行扩展, 见 15-17 章
factory(name, provider) 创建一个服务, 见 14 章
filter(name, factory) 创建一个对显示给用户的数据进行格式化的过滤器.见 14 章
provider(name, type) 创建一个服务.
name 返回模块名称
run(callback) 注册一个在 Angular 加载完毕后用于对所有模块进行配置的函数.
service(name, constructor) 创建一个服务
value(name, value) 定义一个返回一个常量的服务.
factory, service, provider 的区别 见 14,18 章

可以按照任何顺序创建组件, AngularJS 将保证在开始调用工厂函数和执行依赖注入之前一切都已正确创建.

2. fluent API

Module 对象定义的方法返回的结果仍然是 Module 对象本身. 这是的能够使用 fluent API , 即多个方法调用可以链式调用链接在一起.
如下示例:

<script type="text/javascript">
    angular.module('exampleApp', [])
        .controller('dayCtrl', ['$scope', function ($scope) {
            var dayName = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Staturday"]
            $scope.day = dayName[new Date().getDay()];
        }])
        .controller('tomorrowCtrl', ['$scope', function ($scope) {
            var dayName = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Staturday"]
            $scope.day = dayName[(new Date().getDay() + 1) % 7]
        }])
</script>

# angular.module 方法得到 Module 对象作为返回结果, 在这个对象上直接调用 controller 方法创建 dayCtrl 控制器. 从 controller 方法得到的结果与调用 angular.module 方法得到的结果是同一个 Module 对象, 所以可以使用它调用 controller 方法来创建 tomorrowCtrl .

3. ng-app 与 模块

将 ng-app 指令应用到 HTML 中. ng-app 属性是在 AngularJS 生命周期的 bootstrap 阶段被使用.

4. 模块创建/查找陷阱

var myApp = angular.module('exampleApp');       # 查找名称为 exampleApp 的模块
var myApp = angular.module('exampleApp', []);   # 创建名称为 exampleApp 的模块

二. 使用模块组织代码

1. 模块依赖

任何 AngularJS 模块都可以依赖于在其他模块中定义的组件, 在复杂的应用程序中这是一个能够使得组织代码更为容易的特性.

模块的定义可以按照任何顺序定义. AngularJS 会加载定义在程序中的所有模块并解析依赖, 将每个模块中包含的构件进行合并.这个合并使得无缝的使用来来自其他模块的功能成为可能.

<script>
    var controllersModule = angular.module("exampleApp.Controllers", [])

    controllersModule.controller("dayCtrl", function ($scope, days) {
        $scope.day = days.today;

    });

    controllersModule.controller("tomorrowCtrl", function ($scope, days) {
        $scope.day = days.tomorrow;
    });

    angular.module("exampleApp.Filters", []).filter("dayName", function () {
        var dayNames = ["Sunday", "Monday", "Tuesday", "Wednesday",
                        "Thursday", "Friday", "Saturday"];
        return function (input) {
            return angular.isNumber(input) ? dayNames[input] : input;
        };
    });      

    var myApp = angular.module("exampleApp",
        ["exampleApp.Controllers", "exampleApp.Filters",
         "exampleApp.Services", "exampleApp.Directives"]);  // 依赖注入. 

    angular.module("exampleApp.Directives", [])
        .directive("highlight", function ($filter) {

            var dayFilter = $filter("dayName");

            return function (scope, element, attrs) {
                if (dayFilter(scope.day) == attrs["highlight"]) {
                    element.css("color", "red");
                }
            }
        });

    var now = new Date();
    myApp.value("nowValue", now);

    angular.module("exampleApp.Services", [])
        .service("days", function (nowValue) {
            this.today = nowValue.getDay();
            this.tomorrow = this.today + 1;
        });

</script>

// 模块的定义可以按照任何顺序定义. AngularJS 会加载定义在程序中的所有模块并解析依赖, 将每个模块中包含的构件进行合并.这个合并使得无缝的使用来来自其他模块的功能成为可能.如 exmapleApp.service 模块中的 days 服务依赖来自于 exampleApp 模块中的 nowValue 值服务, exmapleApp.directives 模块中指令依赖于来自 exampleApp.filter 模块中的过滤器.

2. Module.config

传给 config 方法的函数在当前模块被加载后调用;

config 方法接受一个函数, 该函数在调用方法的模块被加载后调用. config 方法通常通过注入来自其他服务的值的方式用于配置模块.

3. Module.run

传给 run 方法的函数在所有模块被加载后调用

run 方法接受的函数只会在所有模块加载完成后以及解析完他们的依赖后才会被调用.

<script>

    var myApp = angular.module("exampleApp",
        ["exampleApp.Controllers", "exampleApp.Filters",
            "exampleApp.Services", "exampleApp.Directives"]);

    // constant 方法与 value 方法类似, 但是创建的能够作为 config 方法所声明的依赖使用 (value 服务做不到).
    myApp.constant("startTime", new Date().toLocaleTimeString());

    myApp.config(function (startTime) {
        console.log("Main module config: " + startTime);
    });
    myApp.run(function (startTime) {
        console.log("Main module run: " + startTime);
    });

    angular.module("exampleApp.Directives", [])
        .directive("highlight", function ($filter) {

            var dayFilter = $filter("dayName");

            return function (scope, element, attrs) {
                if (dayFilter(scope.day) == attrs["highlight"]) {
                    element.css("color", "red");
                }
            }
        });

    var now = new Date();
    myApp.value("nowValue", now);

    angular.module("exampleApp.Services", [])
        .service("days", function (nowValue) {
            this.today = nowValue.getDay();
            this.tomorrow = this.today + 1;
        })
        .config(function() {
            console.log("Services module config: (no time)");
        })
        .run(function (startTime) {
            console.log("Services module run: " + startTime);
        });

</script>