0

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.

demo.b
  • 3,299
  • 2
  • 29
  • 29
  • 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
  • @demo.b You can read this https://gist.github.com/domenic/3889970 Also it is clearly explained in many answers in the linked question. You may want to read about AJAX and how it works. Your issue is not angular specific, it is more of how to return/use response from ajax call – PSL Jul 17 '15 at 13:30

1 Answers1

1

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

Mathew Berg
  • 28,625
  • 11
  • 69
  • 90
  • 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