Why use services in Angular?

前端 未结 5 1072
甜味超标
甜味超标 2020-12-06 05:15

I am just starting out with Angular. Reading the example of a service in the Google documentation, I just wonder why you would choose to use a service rather keeping the var

5条回答
  •  天命终不由人
    2020-12-06 06:05

    When you want to keep a variable in memory (why not, a user session for instance) during navigation (differents partials with diffents controller are called). You have to keep in mind :

    • Controllers are re-initialized each time you call them.

    • Services are constructed only one time and are always available. So you can persist any information you need.

提交回复
热议问题