第一句子网 - 唯美句子、句子迷、好句子大全
第一句子网 > 对服务与工厂感到困惑

对服务与工厂感到困惑

时间:2024-05-07 08:30:33

相关推荐

对服务与工厂感到困惑

本文翻译自:Confused about Service vs Factory

As I understand it, when inside a factory I return an object that gets injected into a controller.据我了解,当在工厂内部时,我返回一个被注入控制器的对象。When inside a service I am dealing with the object usingthisand not returning anything.在服务内部时,我使用this处理对象而不返回任何内容。

I was under the assumption that a service wasalways a singleton, and that anew factory objectgets injected in every controller.我假设服务总是单例,并且每个控制器都会注入一个新的工厂对象。However, as it turns out, a factory object is a singleton too?然而,事实证明,工厂对象也是单身人士?

Example code to demonstrate:用于演示的示例代码:

var factories = angular.module('app.factories', []);var app = angular.module('app', ['ngResource', 'app.factories']);factories.factory('User', function () {return {first: 'John',last: 'Doe'};});app.controller('ACtrl', function($scope, User) {$scope.user = User;});app.controller('BCtrl', function($scope, User) {$scope.user = User;});

When changinguser.firstinACtrlit turns out thatuser.firstinBCtrlis also changed, egUseris a singleton?当改变user.firstACtrl事实证明,user.firstBCtrl也发生了变化,例如,User是单身?

My assumption was that a new instance was injected in a controller with a factory?我的假设是在一个带有工厂的控制器中注入了一个新实例?

#1楼

参考:/question/vkBQ/对服务与工厂感到困惑

#2楼

All angular services are singletons:所有角度服务都是单身人士

Docs (seeServices as singletons): /guide/services文档(请参阅单项服务): https : ///guide/services

Lastly, it is important to realize that all Angular services are application singletons.最后,重要的是要意识到所有Angular服务都是应用程序单例。This means that there is only one instance of a given service per injector.这意味着每个注入器只有一个给定服务的实例。

Basically the difference between the service and factory is as follows:基本上服务和工厂之间的区别如下:

app.service('myService', function() {// service is just a constructor function// that will be called with 'new'this.sayHello = function(name) {return "Hi " + name + "!";};});app.factory('myFactory', function() {// factory returns an object// you can run some code beforereturn {sayHello : function(name) {return "Hi " + name + "!";}}});

Check out this presentation about $provide: /1113/#/查看有关$ offer的演示文稿: http : ///1113/#/

Those slides were used in one of the AngularJs meetups: //11/more-angularjs-meetup-videos.html这些幻灯片用于AngularJs的一次聚会: http : ////11/more-angularjs-meetup-videos.html

#3楼

There is also a way to return a constructor function so you can returnnewableclasses in factories, like this:还有一种方法可以返回构造函数,以便您可以在工厂中返回新的类,如下所示:

function MyObjectWithParam($rootScope, name) {this.$rootScope = $rootScope;this.name = name;}MyObjectWithParam.prototype.getText = function () {return this.name;};App.factory('MyObjectWithParam', function ($injector) {return function(name) { return $injector.instantiate(MyObjectWithParam,{ name: name });};});

So you can do this in a controller, which uses MyObjectWithParam:所以你可以在一个使用MyObjectWithParam的控制器中做到这一点:

var obj = new MyObjectWithParam("hello"),

See here the full example:在这里看到完整的例子:

http://plnkr.co/edit/GKnhIN?p=previewhttp://plnkr.co/edit/GKnhIN?p=preview

And here the google group pages, where it was discussed:在这里谷歌小组页面,讨论的地方:

/forum/#!msg/angular/56sdORWEoqg/b8hdPskxZXsJ/forum/#!msg/angular/56sdORWEoqg/b8hdPskxZXsJ

#4楼

Adding to the first answer, I think .service() is for people who have written their code in more object oriented style(C#/Java) (using this keyword and instantiating object via prototype/Constructor function).添加到第一个答案,我认为.service()适用于以更面向对象的方式编写代码的人(C#/ Java)(使用此关键字并通过prototype / Constructor函数实例化对象)。

Factory is for developers who write code which is more natural to javascript/functional style of coding.工厂适用于编写代码的开发人员,这些代码对于javascript /功能编码风格更为自然。

Take a look at the source code of .service and .factory method inside angular.js - internally they all call provider method:看看angular.js里面的.service和.factory方法的源代码 - 在内部它们都调用了provider方法:

function provider(name, provider_) {if (isFunction(provider_)) {provider_ = providerInjector.instantiate(provider_);}if (!provider_.$get) {throw Error('Provider ' + name + ' must define $get factory method.');}return providerCache[name + providerSuffix] = provider_;}function factory(name, factoryFn) { \return provider(name, { $get: factoryFn }); }function service(name, constructor) {return factory(name, ['$injector', function($injector) {return $injector.instantiate(constructor);}]);}

#5楼

live example实例

" hello world " example“你好世界”的例子

withfactory/service/provider:与factory/service/provider

var myApp = angular.module('myApp', []);//service style, probably the simplest onemyApp.service('helloWorldFromService', function() {this.sayHello = function() {return "Hello, World!"};});//factory style, more involved but more sophisticatedmyApp.factory('helloWorldFromFactory', function() {return {sayHello: function() {return "Hello, World!"}};});//provider style, full blown, configurable versionmyApp.provider('helloWorld', function() {// In the provider function, you cannot inject any// service or factory. This can only be done at the// "$get" method.this.name = 'Default';this.$get = function() {var name = this.name;return {sayHello: function() {return "Hello, " + name + "!"}}};this.setName = function(name) {this.name = name;};});//hey, we can configure a provider! myApp.config(function(helloWorldProvider){helloWorldProvider.setName('World');});function MyCtrl($scope, helloWorld, helloWorldFromFactory, helloWorldFromService) {$scope.hellos = [helloWorld.sayHello(),helloWorldFromFactory.sayHello(),helloWorldFromService.sayHello()];}​

#6楼

Here are the primary differences:以下是主要差异:

Services服务

Syntax:module.service( 'serviceName', function );语法:module.service( 'serviceName', function );

Result: When declaring serviceName as an injectable argument you will be provided with theinstance of a functionpassed tomodule.service.结果:将serviceName声明为injectable参数时,将向您提供传递给module.service的函数实例

Usage: Could be useful forsharing utility functionsthat are useful to invoke by simply appending () to the injected function reference.用法:通过简单地向注入的函数引用附加()来共享对调用有用的实用程序函数非常有用。Could also be run withinjectedArg.call( this )or similar.也可以使用injectedArg.call( this )或类似的方式运行。

Factories工厂

Syntax:module.factory( 'factoryName', function );语法:module.factory( 'factoryName', function );

Result: When declaring factoryName as an injectable argument you will be provided thevalue that is returned by invoking the function referencepassed tomodule.factory.结果:当将factoryName声明为injectable参数时,将通过调用传递给module.factory的函数引用来提供返回

Usage: Could be useful for returning a'class'function that can then be new'ed to create instances.用法:用于返回一个'class'函数,然后可以用来创建实例。

Also check AngularJS documentation and similar question on stackoverflow confused about service vs factory .还要检查AngularJS文档和stackoverflow上关于服务与工厂混淆的类似问题。

Here is example using services and factory .这是使用服务和工厂的示例 。Read more about AngularJS service vs factory .关于AngularJS服务与工厂的信息 。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。