Eachwebapplicationyoubuildiscomposedofobjectsthatcollaboratetogetstuffdone.Theseobjectsneedtobeinstantiatedandwiredtogetherfortheapptowork.InAngularappsmostoftheseobjectsareinstantiatedandwiredtogetherautomaticallybytheinjectorservice.
你创建的任何Web应用都是一些互相依赖的对象组合。这些对象需要被实例化并被绑定在一起工作。在Angular应用中,这些对象通过注入器服务自动完成实例化和绑定。
Theinjectorcreatestwotypesofobjects,servicesandspecializedobjects.
注入器创建了两种类型的对象,services(服务)和specializedobjects(特殊对象)。
ServicesareobjectswhoseAPIisdefinedbythedeveloperwritingtheservice.
服务等同于对象,它的API由编写服务的开发者定义。
SpecializedobjectsconformtoaspecificAngularframeworkAPI.Theseobjectsareoneofcontrollers,directives,filtersoranimations.
特殊对象服从一套专门的Angular框架API。这些对象是控制器、指令、过滤器或动画效果中的一个。
Theinjectorneedstoknowhowtocreatetheseobjects.Youtellitbyregisteringa"recipe"forcreatingyourobjectwiththeinjector.Therearefiverecipetypes.
注入器需要知晓如何去创建这些对象。你通过注册一个“recipe(配方)”来告诉注入器去创建你的对象。共有五种类型的配方。
Themostverbose,butalsothemostcomprehensiveoneisaProviderrecipe.Theremainingfourrecipetypes—Value,Factory,ServiceandConstant—arejustsyntacticsugarontopofaProviderrecipe.
最繁琐,也是功能最全面的是Providerrecipe。剩下的四种类型——Value,Factory,Service和Constant——仅仅是Providerrecipe的语法糖。
Let'stakealookatthedifferentscenariosforcreatingandusingservicesviavariousrecipetypes.We'llstartwiththesimplestcasepossiblewherevariousplacesinyourcodeneedasharedstringandwe'llaccomplishthisviaValuerecipe.
接下来,我们看看如何在不同场景下通过不同的recipetypes创建和使用services。我们将从最简单的例子开始,通过Valuerecipe在代码中共享一个字符串。
Note:AWordonModules
注意:模块概要
Inorderfortheinjectortoknowhowtocreateandwiretogetheralloftheseobjects,itneedsaregistryof"recipes".Eachrecipehasanidentifieroftheobjectandthedescriptionofhowtocreatethisobject.
为了让注入器知晓如何创建和绑定所有的对象,它需要一个"recipes"的注册表。每个recipe都有唯一的对象标识符和以及何创建这个对象的描述。
EachrecipebelongstoanAngularmodule.AnAngularmoduleisabagthatholdsoneormorerecipes.Andsincemanuallykeepingtrackofmoduledependenciesisnofun,amodulecancontaininformationaboutdependenciesonothermodulesaswell.
WhenanAngularapplicationstartswithagivenapplicationmodule,Angularcreatesanewinstanceofinjector,whichinturncreatesaregistryofrecipesasaunionofallrecipesdefinedinthecore"ng"module,applicationmoduleanditsdependencies.Theinjectorthenconsultsthereciperegistrywhenitneedstocreateanobjectforyourapplication.
一个Angular应用开始于一个给定的应用模块时,Angular会创建一个新的注入器实例,进而按照所有核心"ng"模块、应用模块和在它的依赖中统一定义的recipes来创建一个recipes的注册表。然后,注入器通过查询recipes注册表来创建应用所需的对象。
Let'ssaythatwewanttohaveaverysimpleservicecalled"clientId"thatprovidesastringrepresentinganauthenticationidusedforsomeremoteAPI.Youwoulddefineitlikethis:
假定我们想要获得一个非常简单的service叫做"clientId",它提供一个字符串用于表示某些远程API的认证id。你可以这样去定义它:
varmyApp=angular.module('myApp',[]);myApp.value('clientId','a12345654321x');NoticehowwecreatedanAngularmodulecalledmyApp,andspecifiedthatthismoduledefinitioncontainsa"recipe"forconstructingtheclientIdservice,whichisasimplestringinthiscase.
注意,我们创建一个名为myApp的Angular模块,然后指定了一个包含构建clientIdservice的配方,这只是一个字符串的简单例子。
AndthisishowyouwoulddisplayitviaAngular'sdata-binding:
以下是如何通过Angular数据绑定来显示它:
myApp.controller('DemoController',['clientId',functionDemoController(clientId){this.clientId=clientId;}]);