Why should I choose using a Service over a Factory in AngularJS?

This question already has an answer here:

  • AngularJS: Service vs provider vs factory 30 answers

  • In Short

    If you want your function to be called like a normal function, use factory. If you want your function to be instantiated with the new operator, use service. If you don't know the difference, use factory

    Source :

    URL : http://iffycan.blogspot.in/2013/05/angular-service-or-factory.html

    Detailed Explanation

    When you use service , the variables and functions you declare will be prototyped to new object and object will be returned to you. Where in factory , it will return the function simply as it is.

    Lets say you created a service with 10 functions , when you include it as dependency, new object will be created and all function will be prototyped , check below example

    function Gandalf() {
        this.color = 'grey';
    }
    
    Gandalf.prototype.comeBack = function() {
        this.color = 'white';
    }
    

    "this" will be returned. Performance issues are negligible here.

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

    上一篇: 服务和工厂的区别

    下一篇: 为什么我应该选择使用AngularJS中的工厂服务?