Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

My basic premise is I want to call back to the server to get the logged in user in case someone comes to the site and is still logged in. On the page I want to call this method. Since I am passing the user service to all my controllers I don't know which controller will be in use since I won't know what page they're landing on.

I have the following User Service

app.factory('userService', function ($window) {
    var root = {};
    root.get_current_user = function(http){
        var config = {
        params: {}
    };
    http.post("/api/user/show", null, config)
        .success(function(data, status, headers, config) {
            if(data.success == true) {
                user = data.user;

                show_authenticated();
            }

        });
    };
    return root;
});

Here is an empty controller I'm trying to inject the service into

app.controller('myResourcesController', function($scope, $http, userService) {

});

So on the top of my index file I want to have something along the lines of

controller.get_current_user();

This will be called from all the pages though so I'm not sure the syntax here. All examples I found related to calling a specific controller, and usually from within another controller. Perhaps this needs to go into my angularjs somewhere and not simply within a script tag on my index page.

share|improve this question
    
Is there a reason that you can't call that service when Angular initializes? I don't see the reason why you'd need it to be called from everywhere if you only need it for when the page gets refreshed, which is the only time where Angular would initialize. – J_A_X May 1 '14 at 7:26
    
I don't know why I didn't think of that, that's probably the best place to put it. – Jhorra May 1 '14 at 14:00

2 Answers 2

up vote 2 down vote accepted

You could run factory initialization in run method of your angular application. https://docs.angularjs.org/guide/module#module-loading-dependencies E.g.

app.run(['userService', function(userService) {
  userService.get_current_user();
}]);

And userService factory should store authenticated user object internaly.

...
if (data.success == true) {
  root.user = data.user;
}
...

Then you will be able to use your factory in any controller

app.controller('myController', ['userService', function(userService) {
   //alert(userService.user);
}]);
share|improve this answer
    
I'm only storing it externally so other items on the page can interact with it. – Jhorra May 1 '14 at 14:01

You need to inject $http through the factory constructor function, for firsts

app.factory('userService', function ($window, $http) {
    var root = {};
    root.get_current_user = function(){
        var config = {
        params: {}
    };
    $http.post("/api/user/show", null, config)
        .success(function(data, status, headers, config) {
            if(data.success == true) {
                user = data.user;

                show_authenticated();
            }

        });
    };
    return root;
});

in your controller you can say

$scope.get_current_user = UserService.get_current_user();

ng attributes in your html if needed. besides this, i am not sure what you need.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.