创建服务方法时,什么是module.service和module.factory之间的区别 [英] When creating service method what's the difference between module.service and module.factory

查看:474
本文介绍了创建服务方法时,什么是module.service和module.factory之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道什么是最好的做法,我应该用什么。

什么是以下两种方法之间的区别?

  module.service(..);

  module.factory(..);


解决方案

有一个关于这个伟大的谷歌组从后帕维尔​​·科兹洛夫斯基:

<一个href=\"https://groups.google.com/forum/#!msg/angular/hVrkvaHGOfc/idEaEctreMYJ\">https://groups.google.com/forum/#!msg/angular/hVrkvaHGOfc/idEaEctreMYJ

这是鲍威尔引:


  

其实$ provide.provider,$ provide.factory和$ provide.service是
  或多或少在这个意义上,它们都是同样的事情
  创建对象实例蓝图/指令(这些
  实例然后准备被注入到合作者)。


  
  

$ provide.provider是登记最spohisticated方法
  蓝图,它可以让你有一个复杂的创作功能,
  配置选项。


  
  

$ provide.factory为$ provide.provider的简化版本,当你
  不需要支持的配置选项,但仍希望有一个
  更复杂的创建逻辑。


  
  

$ provide.service是那里的整个创作逻辑的情况下沸腾
  到调用构造函数。


  
  

因此​​,取决于你将你的结构逻辑的复杂性
  选择$ provide.provider,$ provide.factory和$ provide.service之一
  但你会得到到底是一个新的实例。


下面是伴随小提琴(从线程)证明:<一href=\"http://jsfiddle.net/pkozlowski_opensource/PxdSP/14/\">http://jsfiddle.net/pkozlowski_opensource/PxdSP/14/

而code:

  VAR对myApp = angular.module('对myApp',[]);//服务作风,这可能是最简单的一种
myApp.service('helloWorldFromService',函数(){
    this.sayHello =功能(){
        返回你好,世界!
    };
});//工厂的风格,更多地参与更复​​杂
myApp.factory('helloWorldFromFactory',函数(){
    返回{
        sayHello的:功能(){
            返回你好,世界!
        }
    };
});//提供商的风格,完全成熟的,可配置的版本
myApp.provider('的helloWorld',函数(){    this.name =默认;    这一点。$ GET =功能(){
        变量名称= this.name;
        返回{
            sayHello的:功能(){
                返回你好,+姓名+!
            }
        }
    };    this.setName =功能(名称){
        this.name =名称;
    };
});//哎,我们可以配置一个供应商!
myApp.config(功能(helloWorldProvider){
    helloWorldProvider.setName(世界);
});
功能MyCtrl($范围的helloWorld,helloWorldFromFactory,helloWorldFromService){    $ scope.hellos = [
        helloWorld.sayHello(),
        helloWorldFromFactory.sayHello(),
        helloWorldFromService.sayHello()];
}

I don't know what is the best practice and what I should use.

What is the difference between below two methods?

module.service(..);

and

module.factory(..);

解决方案

There is a great google group post about this from Pawel Kozlowski:

https://groups.google.com/forum/#!msg/angular/hVrkvaHGOfc/idEaEctreMYJ

Quoted from Powel:

in fact $provide.provider, $provide.factory and $provide.service are more or less the same thing in the sense that all of them are blueprints / instructions for creating object instances (those instances are then ready to be injected into collaborators).

$provide.provider is the most spohisticated method of registering blueprints, it allows you to have a complex creation function and configuration options.

$provide.factory is a simplified version of $provide.provider when you don't need to support configuration options but still want to have a more sophisticated creation logic.

$provide.service is for cases where the whole creation logic boils down to invoking a constructor function.

So, depending on the complexity of your construction logic you would choose one of $provide.provider, $provide.factory and $provide.service but in the end what you are going to get is a new instance.

Here is the accompanying fiddle to demonstrate (from the thread): http://jsfiddle.net/pkozlowski_opensource/PxdSP/14/

And the code:

var myApp = angular.module('myApp', []);

//service style, probably the simplest one
myApp.service('helloWorldFromService', function() {
    this.sayHello = function() {
        return "Hello, World!"
    };
});

//factory style, more involved but more sophisticated
myApp.factory('helloWorldFromFactory', function() {
    return {
        sayHello: function() {
            return "Hello, World!"
        }
    };
});

//provider style, full blown, configurable version     
myApp.provider('helloWorld', function() {

    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()];
}

这篇关于创建服务方法时,什么是module.service和module.factory之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆