What you are experiencing is actually the result of how JavaScript behaves.

A factory will inject whatever the factory function returns.

A service will instantiate an object by calling "new" on the function.

In JavaScript if a function returns an object that object is what is 
returned when "new" is called on it. If the function does not return an 
object "this" object is returned.

Therefore the following function will return the same thing in both cases 
below:

function Person() {
    return { name: "John" };
}

var person1 = Person();
var person2 = new Person();

// person1 = { name: "John" }
// person2 = { name: "John" }

On the other hand the following will not work:

function Person() {
    this.name = "John";
}

var person1 = Person();
var person2 = new Person();

// person1 = undefined
// person2 = { name: "John" }

For this reason the first version of Person which returns an object will 
work the same in both factory and service. Factory uses the return value 
while service invokes "new" on the function. The second approach is the 
more common approach and it would only work with service.

On Friday, November 27, 2015 at 5:19:52 PM UTC-5, binaryMoods wrote:
>
> Hi Sander
>
> Awesome! That was my suspicion. Thank you for taking time to answer my 
> questions!
>

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to