服务公司对安古拉工厂

服务公司对安古拉工厂

我都见过安古拉尔工厂()angular.service()用于声明服务;但是,i找不到 angular.service官方文件里的任何地方。

这两种方法有什么区别?哪一个应该用来做什么(假设他们做不同的事情)?


aluckdog
浏览 492回答 3
3回答

摇曳的蔷薇

angular.service('myService',&nbsp;myServiceFunction); &nbsp;&nbsp;angular.factory('myFactory',&nbsp;myFactoryFunction);我很难把我的头绕在这个概念上,直到我这样对自己说:服务*功能你写的将会是新的-编辑:&nbsp;&nbsp;myInjectedService&nbsp;&nbsp;<----&nbsp;&nbsp;new&nbsp;myServiceFunction()工厂*功能(构造函数)您编写的被调用:&nbsp;&nbsp;myInjectedFactory&nbsp;&nbsp;<---&nbsp;&nbsp;myFactoryFunction()你做什么取决于你自己,但有一些有用的模式.比如写一个服务函数公开公共API:function&nbsp;myServiceFunction()&nbsp;{ &nbsp;&nbsp;this.awesomeApi&nbsp;=&nbsp;function(optional)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;calculate&nbsp;some&nbsp;stuff &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;awesomeListOfValues; &nbsp;&nbsp;}}--------------------------------------------------------------------------------- &nbsp;&nbsp;//&nbsp;Injected&nbsp;in&nbsp;your&nbsp;controller$scope.awesome&nbsp;=&nbsp;myInjectedService.awesomeApi();或者使用工厂函数公开公共API:function&nbsp;myFactoryFunction()&nbsp;{ &nbsp;&nbsp;var&nbsp;aPrivateVariable&nbsp;=&nbsp;"yay"; &nbsp;&nbsp;function&nbsp;hello()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;"hello&nbsp;mars&nbsp;"&nbsp;+&nbsp;aPrivateVariable; &nbsp;&nbsp;} &nbsp;&nbsp;//&nbsp;expose&nbsp;a&nbsp;public&nbsp;API &nbsp;&nbsp;return&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;hello:&nbsp;hello&nbsp;&nbsp;};}--------------------------------------------------------------------------------- &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;Injected&nbsp;in&nbsp;your&nbsp;controller$scope.hello&nbsp;=&nbsp;myInjectedFactory.hello();或者使用工厂函数返回构造函数:function&nbsp;myFactoryFunction()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;function()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;a&nbsp;=&nbsp;2; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.a2&nbsp;=&nbsp;function()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;a*2; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;};}--------------------------------------------------------------------------------- &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;Injected&nbsp;in&nbsp;your&nbsp;controllervar&nbsp;myShinyNewObject&nbsp;=&nbsp;new&nbsp;myInjectedFactory();$scope.four&nbsp;=&nbsp;myShinyNewObject.a2();用哪一个?.你可以用这两种方法完成同样的事情。但是,在某些情况下,工厂为您提供了更多的灵活性,以创建一个具有更简单语法的可注入性。这是因为MyInjectedService必须始终是对象,myInjectedFactory可以是对象、函数引用或任何值。例如,如果您编写了一个服务来创建构造函数(如上面的示例所示),则必须实例化它,如下所示:var&nbsp;myShinyNewObject&nbsp;=&nbsp;new&nbsp;myInjectedService.myFunction()可以说,这比这更不可取:var&nbsp;myShinyNewObject&nbsp;=&nbsp;new&nbsp;myInjectedFactory();(但首先您应该小心使用这种模式,因为新的-控制器中的ing对象创建了难以模拟用于测试的硬跟踪依赖项。与其使用服务,不如让服务为您管理对象集合。new()(诡计多端)还有一件事,他们都是单身.。还请记住,在这两种情况下,角是帮助您管理单例。无论您在何处注入服务或函数,也不管注入多少次,您都将得到对同一个对象或函数的相同引用。(工厂只返回数字或字符串之类的值时除外。在这种情况下,您将始终得到相同的值,而不是引用。)

MYYA

简单地说.。//&nbsp;Serviceservice&nbsp;=&nbsp;(a,&nbsp;b)&nbsp;=>&nbsp;{ &nbsp;&nbsp;a.lastName&nbsp;=&nbsp;b; &nbsp;&nbsp;return&nbsp;a;};//&nbsp;Factoryfactory&nbsp;=&nbsp;(a,&nbsp;b)&nbsp;=>&nbsp;Object.assign({},&nbsp;a,&nbsp;{&nbsp;lastName:&nbsp;b&nbsp;});const fullName = { firstName: 'john' };// Serviceconst lastNameService = (a, b) => {&nbsp; a.lastName = b;&nbsp; return a;};console.log(lastNameService(fullName, 'doe'));// Factoryconst lastNameFactory = (a, b) =>&nbsp;&nbsp; Object.assign({}, a, { lastName: b })console.log(lastNameFactory(fullName, 'doe'));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

AngularJS