module.ngdoc

译自Angular‘s module docs

1.模块

大部分的应用都有一个主要的方法来实例化,链接,引导。angular应用没有这个方法,而是用模块声明来替代。

这种方式的优点:

*程序的声明越详细越容易理解

*单元测试不需要加载所有的模块,有助于模块书写

*

*第三方的代码可以打包作为复用的代码

*模块可以以任何顺序加载(由于模块延迟执行的特性)

2.基本示例

 1 <doc:example module=‘myApp‘>
 2   <doc:source>
 3     <script>
 4       // declare a module
 5       var myAppModule = angular.module(‘myApp‘, []);
 6
 7       // configure the module.
 8       // in this example we will create a greeting filter
 9       myAppModule.filter(‘greet‘, function() {
10        return function(name) {
11           return ‘Hello, ‘ + name + ‘!‘;
12         };
13       });
14
15     </script>
16     <div>
17       {{ ‘World‘ | greet }}
18     </div>
19   </doc:source>
20 </doc:example>

3.推荐的模块划分

*服务模块,服务的声明

*管理模块,声明管理相关

*过滤器模块

*依赖于上述模块的应用级模块(包含初始化代码)

这样划分的原因是在测试时,常常需要忽略初始化代码(很难测试)。把它放在单独的模块,可以只用加载相关测试模块。

<doc:example module=‘xmpl‘>
  <doc:source>
    <script>
      angular.module(‘xmpl.service‘, []).
        value(‘greeter‘, {
          salutation: ‘Hello‘,
          localize: function(localization) {
            this.salutation = localization.salutation;
          },
          greet: function(name) {
            return this.salutation + ‘ ‘ + name + ‘!‘;
          }
        }).
        value(‘user‘, {
          load: function(name) {
            this.name = name;
          }
        });

      angular.module(‘xmpl.directive‘, []);

      angular.module(‘xmpl.filter‘, []);

      angular.module(‘xmpl‘, [‘xmpl.service‘, ‘xmpl.directive‘, ‘xmpl.filter‘]).
        run(function(greeter, user) {
          // This is effectively part of the main method initialization code
          greeter.localize({
            salutation: ‘Bonjour‘
          });
          user.load(‘World‘);
        })

      // A Controller for your app
      var XmplController = function($scope, greeter, user) {
        $scope.greeting = greeter.greet(user.name);
      }
    </script>
    <div ng-controller="XmplController">
      {{ greeting }}!
    </div>
  </doc:source>
 </doc:example>

4.模块加载和依赖

最简单的模块包含配置块和运行块,在应用的引导流程提供依赖。

(1)配置块:在注册和配置阶段执行。只有供应者和常量能够添加到配置块。这是为了防止在服务被完全配置之前意外的实例化。

(2)运行块:在注入被创建后执行,被用来安装应用。只有实例和常量能够注入运行块。这是为了阻止应用运行期间系统进一步

配置。

angular.module(‘myModule‘, []).
  config(function(injectables) { // provider-injector
    // This is an example of config block.
    // You can have as many of these as you want.
    // You can only inject Providers (not instances)
    // into the config blocks.
  }).
  run(function(injectables) { // instance-injector
    // This is an example of a run block.
    // You can have as many of these as you want.
    // You can only inject instances (not Providers)
    // into the run blocks
  });

4.1配置块

模块中一些简便的方法相当于配置块

angular.module(‘myModule‘, []).
  value(‘a‘, 123).
  factory(‘a‘, function() { return 123; }).
  directive(‘directiveName‘, ...).
  filter(‘filterName‘, ...);

// is same as

angular.module(‘myModule‘, []).
  config(function($provide, $compileProvider, $filterProvider) {
    $provide.value(‘a‘, 123);
    $provide.factory(‘a‘, function() { return 123; });
    $compileProvider.directive(‘directiveName‘, ...);
    $filterProvider.register(‘filterName‘, ...);
  });

配置块应用的顺序取决于它们注册的顺序,常量例外(常量在所有配置项之前)

4.2运行块

运行块类似主方法,用于安装应用。在所有的服务的配置和注入被创建后执行。换句话说,配置块在运行块之前引入。每个模块只加载一次,即使有多个其它模块依赖它。

4.3异步加载

时间: 2024-08-17 18:25:05

module.ngdoc的相关文章

Angularjs 源码

'use strict'; /* We need to tell jshint what variables are being exported */ /* global -angular, -msie, -jqLite, -jQuery, -slice, -push, -toString, -ngMinErr, -_angular, -angularModule, -nodeName_, -uid, -lowercase, -uppercase, -manualLowercase, -man

Python学习:模块(module)

为了编写可维护的代码,我们把很多函数分组,分别放到不同的文件里,这样,每个文件包含的代码就相对较少,很多编程语言都采用这种组织代码的方式.在Python中,一个.py文件就称之为一个模块(Module). 为什么使用模块? 和C语言类似: 1.大大提高代码的可维护性: 2.很多功能代码可以复用,可以被第三方引用: 3.不同的模块拥有不同的命名空间,可以避免函数名.变量名冲突. 要是不同的人编写的模块名相同怎么办?为了避免冲突,Python引入了按目录来组织模块的方法,成为包(package).

AttributeError: &#39;module&#39; object has no attribute &#39;dumps&#39;

报错: [[email protected] ~]# ./json.py DATA: [{'a': 'A', 'c': 3.0, 'b': (2, 4)}] Traceback (most recent call last): File "./json.py", line 4, in <module> import json File "/root/json.py", line 8, in <module> data_string = jso

nginx安装报错./configure: error: the http rewrite module requires the pcre library.

nginx编译时报错: ./configure: error: the http rewrite module requires the pcre library. 解决方法: [[email protected] nginx-1.5.9]#  yum install zlib-devel -y

@野兽的Angular Api 学习、翻译及理解 - - angular.module

@野兽的 ng api 学习 -- angular.module angular.module 创建一个全局的可用于检索和注入的Angular模块.所有Angular模块(Angular核心模块或者第三方模块)想要在应用里实现,都需要使用这个注入机制. 格式:angular.module(name,[requires],[configFn]); name :  string  创建的模块名称. [requires]: 字符串的数组  代表该模块依赖的其他模块列表,如果不依赖其他模块,则为空数组.

How to Blacklist a Kernel Module in CentOS

How to Blacklist a Kernel Module in CentOS How to Blacklist a Kernel Module in CentOS is an important piece of knowledge for the sysadmin. In Centos Linux, preventing a kernel module from loading during boot is also called blacklisting. We add the mo

(七)理解angular中的module和injector,即依赖注入

依赖注入(DI)的好处不再赘言,使用过spring框架的都知道.angularjs作为前台js框架,也提供了对DI的支持,这是javascript/jquery不具备的特性.angularjs中与DI相关有angular.module().angular.injector(). $injector.$provide.对于一个DI容器来说,必须具备3个要素:服务的注册.依赖关系的声明.对象的获取.比如spring中,服务的注册是通过xml配置文件的<bean>标签或是注解@Repository.

apache添加模块时报错:module status_module is built-in and can&#39;t be loaded

在使用cacti监控linux主机上的apache时,apache需要加载 mod_status.so 模块. 编辑httpd.conf,手动添加下行: LoadModule status_module modules/mod_status.so 在重启apache时报错如下: httpd: Syntax error on line 58 of /etc/httpd/httpd.conf: module status_module is built-in and can't be loaded

saltstack &quot;ImportError: No module named salt.scripts&quot;错误解决

一.问题描述 生产线上使用的自动化管理工具是saltstack,CentOS6.8 x64,python 2.6.6 ,正所谓不作不死,由于有些功能需要python2.7.x ,在升级好python2.7后,写了saltstack api 发现python2.7不好使,原理是python2.7下没有salt模块,因此pip install salt 即在python2.7下安装成功了salt模块,但是发现重启salt-master时报错.如下图: 生产线啊,这可不行,赶紧恢复吧.pip unin