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

前端 未结 4 996
隐瞒了意图╮
隐瞒了意图╮ 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:05

    Just to add on my comment and your edit. Here is what the code would look like if you wanted to be able to re-use your user service and insert it into other apps.

    angular.module('user', []).service('userService', [function(){
       //declare your user properties and methods
    }]) 
    
    angular.module('myApp', ['user'])
    .controller('myCtrl', ['userService', '$scope', function(userService, scope){
        // you can access userService from here
    }])
    

    Not sure if that's what you wanted but likewise you could have your "user" module have a dependency to another "parent" module and access that module's data the same way.

提交回复
热议问题