标签:
Angular services are substitutable objects that are wired together using dependency injection (DI). You can use services to organize and share code across your app.
Angular services are:
Lazily instantiated - Angular only instantiates a service when an application component depends on it.
Singletons - Each component dependent on a service gets a reference to the single instance generated by the service factory.
Angular offers several useful services (like $http), but for most applications you‘ll also want to create your own.
Angular服务是使用依赖注入串起来的可替换的对象。你可以用服务来为你的应用组织和共享代码。
Angular的服务有这些特性:
懒初始化:Angular只有在应用中某个组件依赖它的时候才初始化
单例:每个依赖某服务的组件只获取该服务的一个引用,这些引用都指向服务工厂创建的那个实例(单例)
Note: Like other core Angular identifiers, built-in services always start with $ (e.g. $http).
To use an Angular service, you add it as a dependency for the component (controller, service, filter or directive) that depends on the service. Angular‘s dependency injection takes care of the rest.
咋使用服务呢?给你的组件添加一个对它的依赖就可以了。剩下的工作交给Angular的依赖注入机制即可。
index.html:
1 <div id = "simple" ng-controller = "MyController" > 2 <p> Let‘s try this simple notify service, injected into the controller... </p> 3 <input ng-init = "message=‘test‘" ng-model = "message" > 4 <button ng-click = "callNotify(message);" > NOTIFY </button> 5 <p> (you have to click 3 times to see an alert) </p> 6 </div>
script.js:
1 angular . 2 module ( ‘myServiceModule‘ , []). 3 controller ( ‘MyController‘ , [ ‘$scope‘ , ‘notify‘ , function ( $scope , notify ) { 4 $scope . callNotify = function ( msg ) { 5 notify ( msg ); 6 }; 7 }]). 8 factory ( ‘notify‘ , [ ‘$window‘ , function ( win ) { 9 var msgs = []; 10 return function ( msg ) { 11 msgs . push ( msg ); 12 if ( msgs . length == 3 ) { 13 win . alert ( msgs . join ( "\n" )); 14 msgs = []; 15 } 16 }; 17 }]);
Creating Services(创建服务)
Application developers are free to define their own services by registering the service‘s name and service factory function, with an Angular module.
The service factory function generates the single object or function that represents the service to the rest of the application. The object or function returned by the service is injected into any component (controller, service, filter or directive) that specifies a dependency on the service.
开发者可以通过把服务的名字和工厂方法注册进一个Angular模块中,来定义自己的服务。
服务工厂方法是用来创建这个服务的单例对象或方法的。这个创建出来的服务(无论是个对象还是方法)就会被注入到依赖它的组件中去。
Services are registered to modules via the Module API. Typically you use the Module factory API to register a service:
1 var myModule = angular . module ( ‘myModule‘ , []); 2 myModule . factory ( ‘serviceId‘ , function () { 3 var shinyNewServiceInstance ; 4 // factory function body that constructs shinyNewServiceInstance 5 return shinyNewServiceInstance ; 6 });
Note that you are not registering a service instance, but rather a factory function that will create this instance when called.
可以通过Module API来注册服务。一般情况下,使用Module的factory API来注册。
注意啊,你注册的不是服务的实例,而是一个能创建服务实例的工厂函数。
Services can have their own dependencies. Just like declaring dependencies in a controller, you declare dependencies by specifying them in the service‘s factory function signature. The example module below has two services, each with various dependencies:
服务也可以有自己的依赖项。类似于控制器中声明依赖关系,我们在注册服务的时候,也可以使用类似的写法来为服务声明依赖项。
1 var batchModule = angular . module ( ‘batchModule‘ , []); 2 /** 3 * The ‘batchLog‘ service allows for messages to be queued in memory and flushed 4 * to the console.log every 50 seconds. 5 * 6 * @param {*} message Message to be logged. 7 */ 8 batchModule . factory ( ‘batchLog‘ , [ ‘$internal‘ , ‘$log‘ , function ( $internal , $log ) { 9 var messageQueue = []; 10 function log () { 11 if ( messageQueue . length ) { 12 $log . log ( ‘batchLog message: ‘ , messageQueue ); 13 messageQueue = []; 14 } 15 } 16 // start periodic checking 17 $internal ( log , 50000 ); 18 return function ( message ) { 19 messageQueue . push ( message ); 20 } 21 }]); 22 /** 23 * ‘routeTemplateMonitor‘ monitors each ‘$route‘ change and logs the current 24 * template via the ‘batchLog‘ service. 25 */ 26 batchModule . factory ( ‘routeTemplateMonitor‘ , [ ‘$route‘ , ‘batchLog‘ , ‘$rootScope‘ , 27 function ( $route , batchLog , $rootScope ) { 28 $rootScope . $on ( ‘$routeChangeSuccess‘ , function () { 29 batchLog ( $route . current ? $route . current . template : null ); 30 }); 31 }]);
In the example, note that:
The batchLog service depends on the built-in $internal and $log services.
The routeTemplateMonitor service depends on the built-in $route service and our custom batchLog service.
Both services use the array notation to declare their dependencies.
The order of identifiers in the array is the same as the order of argument names in the factory function.
上面的例子里,注意下:
batchLog服务依赖于内置服务$internal和$log
routeTemplateMonitor服务依赖于内置的$route服务,也依赖于我们的batchLog服务
这俩自定义服务都是使用array记法声明的依赖关系
依赖注入的顺序和工厂函数的参数顺序一定要对上
You can also register services via the $provide service inside of a module‘s config function:
angular . module ( ‘myModule‘ , []). config ([ ‘$provide‘ , function ( $provide ) { $provide . factory ( ‘serviceId‘ , function () { var shinyNewServiceInstance ; // factory function body that constructs shinyNewServiceInstance return shinyNewServiceInstance ; }); }]);
This technique is often used in unit tests to mock out a service‘s dependencies.
当然,你也可以在模块的config方法里,使用$provide来注册服务。
不过这种技术通常用于单元测试中,模拟服务依赖项的时候用。
标签:
原文地址:http://www.cnblogs.com/amile1860/p/4764496.html