2

First of all I want to clear that I am new to AngularJS. You might find that my question is duplicate with AngularJS: 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.

Angular 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>    

Fiddle Link: http://jsfiddle.net/8Cg2s/

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

Community
  • 1
  • 1
dotnetstep
  • 17,065
  • 5
  • 54
  • 72
  • I explain in great detail the difference between the three and why there are three different options even though they seemingly do the same thing here. http://stackoverflow.com/questions/15666048/angular-js-service-vs-provider-vs-factory/23683176#23683176 – Tyler McGinnis Jun 21 '14 at 18:02
  • @TylerMcGinnis I really appreciate for you link. Actually I am learning Angular JS and during search I came across your blog and stackoverflow answer and I tried implement thing found similarity so I ask question. – dotnetstep Jun 22 '14 at 12:09
  • Cool I'm glad you enjoyed it. If you have any questions just shoot me and answer here or on the blog. – Tyler McGinnis Jun 22 '14 at 16:17
  • Seems the core thing that is confusing you is that the value is the same in all the controllers. I get the impression you believe that that the provider `$get()` function and the factory get invoked each time they are injected. This is not the case. They only get called once ever. After the first time it gets called Angular caches the results and providers the same exact object to all subject injections. You can test this by including a console.log() in your function. For examples see: http://www.simplygoodcode.com/2015/11/the-difference-between-service-provider-and-factory-in-angularjs/ – Luis Perez Nov 19 '15 at 20:04

1 Answers1

1

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

Regarding your question: "Why there is diffrent terminolology for three almost identical things ?"

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).


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)".

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.

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.

etc

gkalpak
  • 47,844
  • 8
  • 105
  • 118