是工厂,服务和供应商之间的区别? [英] what is difference between factory, service and provider?

查看:137
本文介绍了是工厂,服务和供应商之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有我想清楚,我是新来的AngularJS第一。你可能会发现,我的问题是重复使用<一个href=\"http://stackoverflow.com/questions/15666048/angular-js-service-vs-provider-vs-factory\">Angular.js:服务提供商VS VS工厂?但我想了解的东西这让我感到困惑。由于我改变一个控制器值在其他的控制器也会受到影响。

First of all I want to clear that I am new to AngularJS. You might find that my question is duplicate with Angular.js: service vs provider vs factory? but as I am trying to understand the thing It get me confused. As I am changing value in one controller get affected in other controller as well.

根据这个问题的答案服务对象由角它的自我创造,但仍然得到所有控制器之间共享。

As per answer to that question service is object created by angular it self but still it get shared between all controller.

角JS

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

    // Factory Implementation
    myApp.factory('myFactory', function () {                      
        var service = {};
        service.TestData = 'This is default data from factory';
        return service;
    });

    // Service Implementation
    myApp.service('myService', function () {
        this.TestData = 'This is service';
    });

    // First Controller
    myApp.controller('myFirstController', function ($scope, myFactory, myService,myProvider) {
        $scope.ProviderData = myProvider;
        $scope.MyData = myFactory;
        $scope.ServiceData = myService;
        $scope.testFun = function () {
            $scope.MyData.TestData = 'This is new data from factory';
            $scope.ServiceData.TestData = 'New Service data';
            $scope.ProviderData.thingOnConfig = 'New thing from first controller';
        }
    });


    // Second Controller
    myApp.controller('mySecondController', function ($scope, myFactory, myService,myProvider) {
        $scope.ProviderData = myProvider;
        $scope.MyData = myFactory;
        $scope.ServiceData = myService;
    });

    myApp.provider('myProvider', function () {
        this.TestData = 'This is from provider';

        this.$get = function () {
            var that = this;
            return {
                thingOnConfig: that.TestData
            }
        }
    });

    myApp.config(function (myProviderProvider) {
        myProviderProvider.TestData = 'This is new from config of provider';
    });

HTML

<div class="row" ng-app="demo" ng-cloak>
    <div class="row" ng-controller="myFirstController">
        <div class="row">
            {{MyData.TestData}}
            <br />
            {{ServiceData.TestData}}
            <br />
            {{ProviderData.thingOnConfig}}
        </div>
        <div class="row">
            <input type="button" value='click here for update' ng-click="testFun()" />
        </div>
    </div>
    <div class="row" ng-controller="mySecondController">
        <div class="row">
            {{MyData.TestData}}
            <br />
            {{ServiceData.TestData}}
            <br />
            {{ProviderData.thingOnConfig}}
        </div>
    </div>
</div>    

小提琴链接: http://jsfiddle.net/8Cg2s/

为什么没有对所有最相同的事情3不同势terminolology?如果有任何重要的区别又是什么呢?

Why there is three diffrent terminolology for all most identical thing? If there is any vital difference then what is that?

推荐答案

小提琴演示了预期的行为。我不明白你混淆

The fiddle demonstrates the expected behaviour. I don't understand what confuses you.

关于你的问题:为什么?有三个几乎一模一样的东西不同势terminolology

如果我们3几乎相同的事情使用完全相同的名称,那么怎么会,我们区分它们?它是唯一合理用于不同的东西不同的充名称(即使它们是非常相似)。

If we used the exact same name for 3 almost identical things, then how would we distinguish between them ? It is only reasonable to use differnt names for different things (even if they are very similar).

我想,真正的问题不是为什么不同的术语,而是为什么有3个不同的函数(工厂服务提供商服务>))。

I suppose the real question is not "why the different terminology", but "why have 3 different functions that (factory, service, provider) for the same purpose (declaring an Angular Service)".

您可能会失望的学习不会有3,但5种方法来声明一个角度服务:是2人失踪功能。

You might be dissapointed to learn there are not 3 but 5 ways to declare an Angular Service: constant and value are the 2 missing functions.

事实上,只有一个概念,一个角服务,并申报一一方式:提供商

In fact there is only one concept, an Angular Service, and one way to declare one: provider.

由其他4个功能(实现任何工厂服务),也可以用提供商,但更多的code实现的。< BR>
提供商是最灵活的(允许最大可配置),也是最详细的。结果
因此,对其他4种功能是常用的类型角度服务。快捷键

Anything achieved by the other 4 functions (constant, factory, service, value) can also be achieved with provider, but with more code.
provider is the most flexible (allowing for the most configurability), but also the most verbose.
Thus, the other 4 functions are shortcuts to commonly used types of Angular Services.

顺便说一句,这是很清楚的 的文档解释

BTW, it is quite clearly explained in the docs:

工厂结果
[...]这是短用于登记服务,其中它的提供仅由$获取属性,这是给定服务工厂功能

factory
[...] This is short for registering a service where its provider consists of only a $get property, which is the given service factory function.

服务结果
[...]这是短期的登记服务,而其提供的$得到财产将用于实例化服务实例的服务构造函数。

service
[...] This is short for registering a service where its provider's $get property is the service constructor function that will be used to instantiate the service instance.

这篇关于是工厂,服务和供应商之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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