AngularJS学习:Angular的模块
原文地址:https://code.angularjs.org/1.3.0-beta.14/docs/guide/module。 模块是什么? 你可以把模块看成是一个你的app的不同部分(controller,services,filters,directives,等等)的一个容器。 为什么?大多数应用有一个main方法,把应用的不同部分实例化和装配在一起。 Angular app没有main方法。取而代之的是模块声明地指定了一个应用应该如何被启动。这种方法有几个优点:
基础我有点着急。我怎么能让一个Hello World模块工作呢?
Edit in Plunker
index.html
script.js
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Example - example-example106-production</title> <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.13/angular.min.js"></script> <script src="script.js"></script> </head> <body ng-app="myApp"> <div> {{ 'World' | greet }} </div> </body> </html> (注:这个地方原本的index.html展示的代码不完整,我将它补全了。) 重要的事情需要注意:
推荐安装尽管上面的例子很简单,但是它可以被扩展成一个大的应用程序。不过我们推荐你把你的应用拆成多个模块,就像这样:
我们已经written a document(编写了一个文档),关于我们在google是如何组织大型的app的。 以上只是一个建议。根据你的需求去进行剪裁。 (注:下面的原文摘抄有点问题,更多请参考Plunker) <div ng-controller"XmplController"> {{ greeting }}! </div> 模块加载 & 依赖一个模块是一个集合,在自启过程中被应用到应用程序上的配置和运行块的集合。最简单的模式就是包含两类块的集合:
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 config blocks. }). run(function(injectables) { // instance-injector // This is an example of a run block. // You can only inject instances (not Providers) // into run blocks }); 配置块模块上有很多方便的方法,可以等同于config块的作用。例如: []). value('a', 123). factory( function() { return 123; }). directive('directiveName', ...). filter('filterName', ...); // is same as angular.module( []). config(function($provide, $compileProvider, $filterProvider) { $provide.value(123); $provide.factory(123; }); $compileProvider.directive( ...); $filterProvider.register( ...); });
在启动的时候,首先Angular应用所有constant的定义。然后Angular用配置块注册的顺序去应用所有配置块。
运行块运行块是Angular中最接近main方法的东西。 运行块是需要运行以启动应用程序的代码。 它在所有服务已配置并且注入器已创建后执行。 运行块通常包含难以进行单元测试的代码,因此应该在隔离模块中声明,以便可以在单元测试中忽略它们。 依赖模块可以列出其他模块作为它们的依赖关系。 根据模块意味着需要的模块需要在加载要求的模块之前加载。 换句话说,所需模块的配置块在需求模块的配置块之前执行。 对于运行块也是如此。 每个模块只能加载一次,即使多个其他模块需要它。 异步加载模块是一种管理$injector配置的方法,与将脚本加载到VM无关。 有现有的项目处理脚本加载,可以与Angular一起使用。 因为模块在加载时什么都不做,所以它们可以按任何顺序加载到VM中,因此脚本加载器可以利用此属性并且并行化加载过程。 创建与检索注意,使用angular.module('myModule',[])将创建模块myModule并覆盖任何名为myModule的现有模块。 使用angular.module('myModule')来检索现有模块。 var myModule = angular.module( []); // add some directives and services myModule.service('myService', ...); myModule.directive('myDirective',136)">// overwrites both myService and myDirective by creating a new module var myModule = angular.module(// throws an error because myOtherModule has yet to be defined var myModule = angular.module('myOtherModule'); 单元测试
单元测试是实例化应用程序的子集以对其应用激励的方式。 小型结构化模块有助于保持单元测试简洁和集中。
每个模块每个进样器只能加载一次。 通常Angular应用程序只有一个注射器,模块只加载一次。 每个测试都有自己的注入器,模块被多次加载。
在所有这些例子中,我们将假设这个模块定义: angular.module('greetMod', []). factory('alert', function($window) { return function(text) { $window.alert(text); } }). value('salutation', 'Hello'). factory('greet', function(alert, salutation) { return function(name) { alert(salutation + ' ' + name + '!'); } }); 让我们编写一些测试来展示如何覆盖测试中的配置。 describe('myApp', function() { // load application module (`greetMod`) then load a special // test module which overrides `$window` with a mock version, // so that calling `window.alert()` will not block the test // runner with a real alert box. beforeEach(module( function($provide) { $provide.value('$window', { alert: jasmine.createSpy('alert') }); })); // inject() will create the injector and inject the `greet` and // `$window` into the tests. it('should alert on $window', inject(function(greet, $window) { greet('World'); expect($window.alert).toHaveBeenCalledWith('Hello World!'); })); // this is another way of overriding configuration in the // tests using inline `module` and `inject` methods. it('should alert using the alert service', function() { var alertSpy = jasmine.createSpy('alert'); module(function($provide) { $provide.value( alertSpy); }); inject(function(greet) { greet('World'); expect(alertSpy).toHaveBeenCalledWith('Hello World!'); }); }); }); 以下是原文: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 BasicsI'm in a hurry. How do I get a Hello World module working?
Edit in Plunker
index.html
script.js
// declare a module var myAppModule = angular.module( []); // configure the module. // in this example we will create a greeting filter myAppModule.filter( function() { return function(name) { return 'Hello,' + name + '!'; }; });
Important things to notice:
Recommended SetupWhile the example above is simple,it will not scale to large applications. Instead we recommend that you break your application to multiple modules like this:
We've alsowritten a documenton how we organize large apps at Google. The above is a suggestion. Tailor it to your needs.
index.html
script.js
<div ng-controller="XmplController"> {{ greeting }}! </div>
Module Loading & DependenciesA module is a collection of configuration and run blocks which get applied to the application during the bootstrap process. In its simplest form the module consist of collection of two kinds of blocks:
'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 config blocks. }). run(function(injectables) { // instance-injector // This is an example of a run block. // You can only inject instances (not Providers) // into run blocks }); Configuration Blocks There are some convenience methods on the module which are equivalent to the []). value('a', 123). factory( function() { return 123; }). directive('directiveName', ...). filter('filterName', ...); // is same as angular.module( []). config(function($provide, $compileProvider, $filterProvider) { $provide.value(123); $provide.factory(123; }); $compileProvider.directive( ...); $filterProvider.register( ...); });
When bootstrapping,first Angular applies all constant definitions. Then Angular applies configuration blocks in the same order they were registered.
Run BlocksRun blocks are the closest thing in Angular to the main method. A run block is the code which needs to run to kickstart the application. It is executed after all of the service have been configured and the injector has been created. Run blocks typically contain code which is hard to unit-test,and for this reason should be declared in isolated modules,so that they can be ignored in the unit-tests. DependenciesModules can list other modules as their dependencies. Depending on a module implies that required module needs to be loaded before the requiring module is loaded. In other words the configuration blocks of the required modules execute before the configuration blocks of the requiring module. The same is true for the run blocks. Each module can only be loaded once,even if multiple other modules require it. Asynchronous LoadingModules are a way of managing $injector configuration,and have nothing to do with loading of scripts into a VM. There are existing projects which deal with script loading,which may be used with Angular. Because modules do nothing at load time they can be loaded into the VM in any order and thus script loaders can take advantage of this property and parallelize the loading process. Creation versus Retrieval Beware that using var myModule = angular.module(// add some directives and services myModule.service('myService', ...); myModule.directive('myDirective',136)">// overwrites both myService and myDirective by creating a new module var myModule = angular.module(// throws an error because myOtherModule has yet to be defined var myModule = angular.module('myOtherModule'); Unit TestingA unit test is a way of instantiating a subset of an application to apply stimulus to it. Small,structured modules help keep unit tests concise and focused.
Each module can only be loaded once per injector. Usually an Angular app has only one injector and modules are only loaded once. Each test has its own injector and modules are loaded multiple times.
In all of these examples we are going to assume this module definition: Let's write some tests to show how to override configuration in tests.'Hello World!'); }); }); }); |
- 每日一shell(七)统计站点的IP和PV
- twitter-bootstrap – Bootstrap 3 input-append升级
- bash – diff的错误退出值是什么?
- 记录tibco webservice的一个错误异常 情况
- 为什么Def.inputTask宏在Scala 2.11.1中不起作用?
- scala – Spark十进制类型精度损失
- 如何使用AngularJS指定optionLabelTemplate Kendo UI DropD
- Angular 4 依赖注入教程之五 FactoryProvider配置依赖对象
- scala – 如何在Spark中将RDD转换为RDD?
- Angular的自定义指令及其实例