Should an Angular service have state?

前端 未结 4 438
南方客
南方客 2021-01-01 10:20

Recently some co-workers and I were having a discussion as to whether or not AngularJS services should have state or not. We came up with some arguments for and against it a

4条回答
  •  盖世英雄少女心
    2021-01-01 10:57

    It would probably depend on what you mean by "state", but in many cases I think the answer would be yes: services should hold state.

    For example, if you have a service that is responsible for communication with an API, that service could hold the authentication state.

    By the way, I'm not sure how much idempotence matters for AngularJS services - they're singletons and so inherently have some state. You could (and in some cases must) create idempotent methods on the service, but that's a separate issue.

提交回复
热议问题