Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

This question already has an answer here:

I'm new to Angularjs and i want to retrieve username from Asp.net MVC controller. In my angular's controller, i have a factory method "getUserName" that gets username from mvc controller. Below is the code

FACTORY

appControllers.factory('testFactory', ['$http', function ($http) {
        var factory = {};

        factory.getUserName = function(){
            return $http.get('Home/GetUser').then(function (result) { return result.data; });
        }

        return factory;
    }]);

In the app controller, i called the factory service "getUserName" to get current active user. Please see code and behavior.

APP CONTROLLER

appControllers.controller('contrlA', ['$scope', '$http', 'testFactory', function ($scope, $http, testFactory) {
     var user = ' ';
        testFactory.getUserName().then(function (result) {
            user = result;
            console.log('print out of console suser ' + user); // this prints my windows login name "print out of console suser AMAZO\demo.b"
        });
        console.log('print out of console user ' + user); // this is the problem, note username is missing "print out of console user"
 }]);

ASP.NET MVC ACTION

 [HttpGet]
        public string GetUser() 
        {
            var userName = User.Identity.Name;

            return userName;
        }

SUMMARY: I want to assign the result of getUserName to a variable so that i can use as input for other methods. Please forgive me if the question is too simple, i'm new to angularjs and have been on this issue for over 4 days.

share|improve this question

marked as duplicate by PSL angularjs Jul 17 '15 at 13:17

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1  
Look at the last answer from @BenjaminGruenbaum – PSL Jul 17 '15 at 13:18
    
Yes i did, please read the comment in the app controller. – demo.b Jul 17 '15 at 13:18
    
Your factory looks like it should be a service instead. Services get instantiated with the new keyword, so you do not need the empty object and the return statement – LionC Jul 17 '15 at 13:18
    
Your issue is due to the asynchronous nature of ajax. There are several duplicates for this. Concept is just the same, it is asyncronous. You seem to be using the promises the right way, but by trying to get the data before the promise resolution you are missing the concept. – PSL Jul 17 '15 at 13:19
    
@PSL please can you explain may be with example. I will appreciate. – demo.b Jul 17 '15 at 13:30
up vote 1 down vote accepted

That's because the http request is asyncrhonus and the console.log that's not working is being hit before the http request finishes.

share|improve this answer
    
i actually passed the var "user" as input to another function and set a break point VS 2013. The user was still null – demo.b Jul 17 '15 at 13:20
    
That's because it's not an object to begin with (you have it as a string) and even if it were you destroyed the reference by doing user = result; – Mathew Berg Jul 17 '15 at 13:22

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