Confused about Service vs Factory

后端 未结 20 2847
轻奢々
轻奢々 2020-11-22 08:49

As I understand it, when inside a factory I return an object that gets injected into a controller. When inside a service I am dealing with the object using this

20条回答
  •  醉话见心
    2020-11-22 09:28

    For me the revelation came when I realise that they all work the same way: by running something once, storing the value they get, and then cough up that same stored value when referenced through Dependency Injection.

    Say we have:

    app.factory('a', fn);
    app.service('b', fn);
    app.provider('c', fn);
    

    The difference between the three is that:

    1. a's stored value comes from running fn , in other words: fn()
    2. b’s stored value comes from newing fn, in other words: new fn()
    3. c’s stored value comes from first getting an instance by newing fn, and then running a $get method of the instance

    which means, there’s something like a cache object inside angular, whose value of each injection is only assigned once, when they've been injected the first time, and where:

    cache.a = fn()
    cache.b = new fn()
    cache.c = (new fn()).$get()
    

    This is why we use this in services, and define a this.$get in providers.

    Hope this helps.

提交回复
热议问题