AngularJs Creating Services详解及示例代码
虽然angular提供许多有用的service,在一些特别的应用中,我们会发现编写自定义service是很有用的。如果我们想做这件事,我们首先要在module中注册一个service工厂方法,可以通过Module.factoryapi(http://docs.angularjs.org/api/angular.module)或者在module配置方法中直接通过$provideapi(http://docs.angularjs.org/api/AUTO.$provide)。
所有angularservice都参与到DI(https://www.nhooo.com/article/91775.htm)中,既可以通过angularDI系统(injector)中使用名称(id)注册自己,也可以通过在其他工厂方法中声明对已存在的service的依赖。
一、RegisteringServices
为了注册一个service,我们必须拥有一个module,并且使这个server成为这个module的一部分。然后,我们可以通过Moduleapi或者在module配置函数中注册service。下面的伪代码将展示这两种注册方式。
使用angular.moduleapi:
varmyModule=angular.module(‘myModule',[]); myModule.factory(‘serviceId',function(){ varsomeService; //工厂方法体,构建someService returnsomeService; });
使用$provideservice:
angular.module(‘myModule',[],function($provide){ $provide.factory(‘serviceId',function(){ varsomeService; //工厂方法体,构建someService returnsomeService; }); });
注意,我们无须注册一个服务实例,相反地,工厂方法会在它被调用的时候被实例化。
二、Dependencies
service不仅仅可以被依赖,更可以拥有自己的依赖。可以在工厂方法的参数中指定依赖。阅读(https://www.nhooo.com/article/91775.htm)更多关于angular中的DI、数组标记的用途和$inject属性,让DI声明更加简洁。(ReadmoreabouttheDIinAngularandtheuseofarraynotationand$injectpropertytomakeDIannotationminification-proof……)
下面是一个非常简单的service例子。这个服务依赖$windowservice(通过工厂方法参数传递),而且只有一个方法。这个service简单地储存所有通知,在第三个之后,这个service会通过window.alert显示所有通知。
<!DOCTYPEHTML> <htmllang="zh-cn"ng-app="MainApp"> <head> <metacharset="UTF-8"> <title>services</title> </head> <body> <divng-controller="MyController"> <inputtype="text"ng-model="msg"/> <buttonng-click="saveMsg()">savemsg</button> <ul> <ling-repeat="msginmsgs">{{msg}}</li> </ul> </div> <scriptsrc="../angular-1.0.1.js"type="text/javascript"></script> <scripttype="text/javascript"> varapp=angular.module("MainApp",[],function($provide){ $provide.factory("notify",["$window","$timeout",function(win,timeout){ varmsgs=[]; returnfunction(msg){ msgs.push(msg); if(msgs.length==3){ timeout(function(){ win.alert(msgs.join("\n")); msgs=[]; },10); } } }]) }); app.controller("MyController",function($scope,notify){ $scope.msgs=[]; $scope.saveMsg=function(){ this.msgs.push(this.msg); notify(this.msg); this.msg=""; }; }); </script> </body> </html>
三、InstantiatingAngularServices
所有在angular中的service都是延迟实例化的(lazily)。这意味着service仅仅在其他依赖它的已实例化的service或者应用组件中被依赖时,才会创建。换句话说,angular直到服务被直接或者间接请求时候,才会实例化service。
四、Servicesassingletons
最后,我们必须意识到所有angularservice都是一个单例应用。这意味着每一个injector中有且只有一个给定service的实例。由于angular是极其讨厌破坏globalstate的,所以创建多个injector,使每一个都有指定service的实例是可行的,除了在测试中有强烈的需求外,一般很少有这样的需要。
以上就是关于AngularServices的资料整理,后续继续补充相关资料,谢谢大家对本站的支持!