Can I or Should I use a Global variable in Angularjs to store a logged in user?

前端 未结 4 1007
隐瞒了意图╮
隐瞒了意图╮ 2020-12-22 08:41

I\'m new to angular and developing my first \'real\' application. I\'m trying to build a calendar/scheduling app ( source code can all be seen on github ) and I want to be a

4条回答
  •  北海茫月
    2020-12-22 09:15

    Yes you can do it in $rootScope. However, I believe it's better practice to put it inside a service. Services are singletons meaning they maintain the same state throughout the application and as such are prefect for storing things like a user object. Using a "user" service instead of $rootScope is just better organization in my opinion. Although technically you can achieve the same results, generally speaking you don't want to over-populate your $rootScope with functionality.

提交回复
热议问题