angular.service vs angular.factory

后端 未结 9 2131
走了就别回头了
走了就别回头了 2020-11-22 02:50

I have seen both angular.factory() and angular.service() used to declare services; however, I cannot find angular.service anywhere in official documentation.

9条回答
  •  别跟我提以往
    2020-11-22 02:59

    All the answers here seem to be around service and factory, and that's valid since that was what was being asked about. But it's also important to keep in mind that there are several others including provider(), value(), and constant().

    The key to remember is that each one is a special case of the other. Each special case down the chain allowing you to do the same thing with less code. Each one also having some additional limitation.

    To decide when to use which you just see which one allows you to do what you want in less code. Here is an image illustrating just how similar they are:

    For a complete step by step breakdown and quick reference of when to use each you can visit the blog post where I got this image from:

    http://www.simplygoodcode.com/2015/11/the-difference-between-service-provider-and-factory-in-angularjs/

提交回复
热议问题