factoy vs service in angular

I have a code which works for both factory and service : I am confused at what circumstances I need to use which code.

This code output my value in console.log using service

var mod = angular.module("MyModule", []);

mod.service("myService", function() {
  console.log("this is service");
    return {
      getvalue : function() {
        return "My value";
      }
  };
});

mod.controller("MyController", function(myService) {
  console.log("MyController - myFactory: " + myService.getvalue());
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="MyModule">
<div ng-controller="MyController"></div>
</div>

This blog post by Todd Motto is worth a thousands answers.

Using .factory() gives us much more power and flexibility, whereas a .service() is essentially the “end result” of a .factory() call. The .service() gives us the returned value by calling new on the function, which can be limiting, whereas a .factory() is one-step before this compile process as we get to choose which pattern to implement and return.

It explains what is the difference at the source code level.

In real life, my opinion is that it's a matter of taste to use one or the other.

Coming from Java, I like the fact that .service looks like a class with the use of this.


Let me explain

Services

Services provide a method for us to keep data around for the lifetime of the app and communicate across controllers in a consistent manner. Services are singleton objects that are instantiated only once per app (by the $injector) and lazyloaded (created only when necessary). They provide an interface to keep together those methods that relate to a specific function.

Factory

This service factory function is responsible for generating a single object or function that becomes this service, which will exist for the lifetime of the app. When our Angular app loads the service, the service will execute this function and hold on to the returned value as the singleton service object. The service factory function can be either a function or an array, just like the way we create controllers:

So i would like you to use factory for your core operations, and call this factory from one service(You need to create one saporate services for this), so this way you can provide more isolation in your app.

Example

*make one factory like :

app.factory('memberServiceFactory', ['$http','$filter', function ($http,$filter) {
    return {
        getData: function (data) {
            return {};
        },           
        addData: function (file, formData) {
        },
    }
}]);

*make one service like :

app.service('memberListService', ['memberServiceFactory', function (memberServiceFactory){
        this.getList = memberServiceFactory.getData;
        this.addMember = memberServiceFactory.addData;               
 }]);

You can see here i am calling factory from services, you can provide encapsulation like this.

链接地址: http://www.djcxy.com/p/77564.html

上一篇: 父母和孩子控制器

下一篇: factoy vs角度服务