AngularJS : What is a factory?

前端 未结 4 1989
误落风尘
误落风尘 2020-12-12 09:37

I\'ve been doing a lot of work on Angular.js and overall I find it to be an interesting and powerful framework.

I know there have been a lot of discussio

4条回答
  •  一生所求
    2020-12-12 10:08

    My two cents on this topic. I am a very very newbie and just into understanding Angular JS and this was one of the things that confused me a lot and hence I studied it in somewhat detail. I have been making notes for giving interviews and this may be useful to others.

    • service and factory do the same things in different ways
    • both are injectibles
    • for most things use factory syntax
    • easier to understand
    • nowadays with es6 "service" is done since it converts to es6 classes better
    • its essentially abstracting away business logic from the controller
    • if you use biz logic with controllers then you can only use with controllers
    • controller is for putting data on scope not processing lengthy biz logic
    • so what happens in above scenario is that complex biz logic is tied up into controllers. Not for processing data. So put bits and pieces of it into services or factory. So your code is lean and modular.
    • services are singletons

提交回复
热议问题