I am new to Angularjs. I am working on my personal website and am using angular. I want people to be able to login. After logging in I want to store there username in a valueservice so that it is available all the time. Can I do it.
I tried doing it in config() function but was not successful. The reason i am using config function is because here i am checking if the user was already logged in.
Thanks.
There are two basic ways of storing data that is supposed to be globally available in Angular.
The first is to store it on the $rootScope. The rootScope is the root of all other scopes (as the name implies), so it is always available in all templates and it can be injected into any controller/service/etc. It is used just like the normal $scope, so a login service would do something like:
app.service('LoginService', ['$rootScope', function($rootScope) {
var user = {}; //Get the user somehow
$rootScope.user = user;
}
The second way is to use a service share persisted data. See the answer here for more info about that.
In this case I would say go with the first, use $rootScope. When you have something that should be globally available the rootScope fits perfectly, if you have something that should be shared between only a few parts of the app, use a service.
A good place to call your login service is in the app.run() method. It is run first thing when the app starts (except after config I believe).