Javascript 角度工厂函数始终返回null

Javascript 角度工厂函数始终返回null,javascript,angularjs,Javascript,Angularjs,我正在尝试将currentUser对象存储在工厂中,以便可以在整个应用程序中访问它。但是,无论何时调用CurrentUserFactory.GetCurrentUser(),它都返回null。我已经验证了服务器端一切正常,并且它正在接收用户对象。但它总是返回null angular.module('myWebApp.services') .factory('CurrentUserFactory', ['SettingsFactory', '$http', function(Setting

我正在尝试将currentUser对象存储在工厂中,以便可以在整个应用程序中访问它。但是,无论何时调用CurrentUserFactory.GetCurrentUser(),它都返回null。我已经验证了服务器端一切正常,并且它正在接收用户对象。但它总是返回null

angular.module('myWebApp.services')
    .factory('CurrentUserFactory', ['SettingsFactory', '$http', function(SettingsFactory, $http) {
            var CurrentUserFactory = {};
            var currentUser = null;

            CurrentUserFactory.GetCurrentUser = function() {
                if (!currentUser) {
                    $http.get(SettingsFactory.myAPIUrl + 'api/users/current', { withCredentials: true }).then(function (response) {
                        currentUser = response.data;
                    });
                }
                return currentUser;
            }

            return CurrentUserFactory;
        }
    ]);

$http
的异步调用返回承诺,但函数本身返回
currentUser
,即
null
。如果
currentUser
null
,则需要返回
$http
承诺,如果不是
null
,则需要返回包装在承诺中的
currentUser

angular.module('myWebApp.services')
    .factory('CurrentUserFactory', ['SettingsFactory', '$http', '$q', function(SettingsFactory, $http, $q) {
            var CurrentUserFactory = {};
            var currentUser = null;

            CurrentUserFactory.GetCurrentUser = function() {
                if (!currentUser) {
                    return $http.get(SettingsFactory.myAPIUrl + 'api/users/current', { withCredentials: true }).then(function (response) {
                        currentUser = response.data;
                        return response.data;
                    });
                }
                return $q.resolve(currentUser);
            }

            return CurrentUserFactory;
        }
    ]);
用法:

由于函数返回承诺,因此需要一个块来获得结果:

CurrentUserFactory.GetCurrentUser().then(function(currentUser) {
    console.log(currentUser);
});

$http
的异步调用返回承诺,但函数本身返回
currentUser
,即
null
。如果
currentUser
null
,则需要返回
$http
承诺,如果不是
null
,则需要返回包装在承诺中的
currentUser

angular.module('myWebApp.services')
    .factory('CurrentUserFactory', ['SettingsFactory', '$http', '$q', function(SettingsFactory, $http, $q) {
            var CurrentUserFactory = {};
            var currentUser = null;

            CurrentUserFactory.GetCurrentUser = function() {
                if (!currentUser) {
                    return $http.get(SettingsFactory.myAPIUrl + 'api/users/current', { withCredentials: true }).then(function (response) {
                        currentUser = response.data;
                        return response.data;
                    });
                }
                return $q.resolve(currentUser);
            }

            return CurrentUserFactory;
        }
    ]);
用法:

由于函数返回承诺,因此需要一个块来获得结果:

CurrentUserFactory.GetCurrentUser().then(function(currentUser) {
    console.log(currentUser);
});

这是一个AJAX调用-您不能从中返回-您需要使用承诺或回调。您可以使用对象初始化您的
currentUser
,然后将所有响应字段复制到该对象。然后你最终会得到一个用户。@tymeJV是
。那么
不是一个承诺?
$http.get
是一个承诺,但你没有返回它。这是一个AJAX调用-你不能从中返回-你需要使用承诺或回调。你可以用对象初始化你的
currentUser
,然后将所有响应字段复制到该对象。然后你最终会得到一个已填充的用户。@tymeJV是
。那么
不是一个承诺?
$http.get
是一个承诺,但你没有返回它。或者
返回承诺。resolve(currentUser)
而不是
$q
dependency它的角度$q是框架的一部分,它还调用apply,哪种角度的双向绑定取决于。我觉得承诺很难看。可以这样做吗:
GetCurrentUser
可以这样使用:
var cu=CurrentUserFactory.GetCurrentUser()?您可以使用。但是,由于IE本机不支持它们,您需要使用babel来传输代码。@Legion,您可以。使用同步ajax。试试看,也许你会发现承诺并不那么难看或者
返回Promise.resolve(currentUser)
而不是
$q
dependency它的angular$q是框架的一部分,它还调用apply,angular双向绑定依赖于它。我觉得promises很难看。可以这样做吗:
GetCurrentUser
可以这样使用:
var cu=CurrentUserFactory.GetCurrentUser()?您可以使用。但是,由于IE本机不支持它们,您需要使用babel来传输代码。@Legion,您可以。使用同步ajax。试试看,也许你会发现承诺并不那么难看