将javascript变量设置为返回函数的值

将javascript变量设置为返回函数的值,javascript,asp.net,angularjs,rest,wcf,Javascript,Asp.net,Angularjs,Rest,Wcf,我试图将控制器中的一个变量设置为函数的返回值。此函数在表中创建一个新条目,然后返回其id。当我在chrome developer tools中调试时,我可以看到我的函数工作正常,并且响应。数据实际上是一个数字。但是,当我尝试为此函数调用设置变量时,该值被设置为未定义 我的AngularJS组件: function saveNewGame($http, gameData) { var newGameData = { "InvestigatorGroupUserId": g

我试图将控制器中的一个变量设置为函数的返回值。此函数在表中创建一个新条目,然后返回其id。当我在chrome developer tools中调试时,我可以看到我的函数工作正常,并且
响应。数据实际上是一个数字。但是,当我尝试为此函数调用设置变量时,该值被设置为未定义

我的AngularJS组件:

function saveNewGame($http, gameData) {

    var newGameData = {
        "InvestigatorGroupUserId": gameData.GroupUserId,
        "InvestigatorGroupGameId": gameData.GroupGameId,
        "WithTeacher": gameData.WithTeacher
    };

    $http.post("/APGame.WebHost/play/newGamePlayed", newGameData)
        .then(function(response) {
            return response.data;
        });

}

function controller($http) {

    var model = this;
    var gameData = model.value;
    var gamePlayedId;

    model.startGame = function() {
        gamePlayedId = saveNewGame($http, gameData);
        alert(gamePlayedId);
    };
}

module.component("gameApp",
{
    templateUrl: "/APGame/GameAngular/game-app.html",
    controllerAs: "game",
    bindings: {
        value: "<"
    },
    controller: ["$http", controller]
});
    [OperationContract]
    [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "newGamePlayed")]
    int NewGamePlayed(GamePlayedData gamePlayedData);



    public int NewGamePlayed(GamePlayedData gamePlayedData)
    {
        var gamePlayedRepo = _gamePlayedRepo ?? new GamePlayedRepository();

        var newGame = new GamePlayed()
        {
            InvestigatorGroupUserId = gamePlayedData.InvestigatorGroupUserId,
            InvestigatorGroupGameId = gamePlayedData.InvestigatorGroupGameId,
            GameStartTime = DateTime.Now,
            IsComplete = false
        };

        return gamePlayedRepo.Create(newGame);
    }

向方法调用添加承诺解析侦听器,如下所示:

model.startGame = function() {
    gamePlayedId = saveNewGame($http, gameData)then(function(response) {
      alert(response.data);
    }, function(reason) {
      alert('Failed: ' + reason);
    });
};
返回http.get承诺,而不是数据

function saveNewGame($http, gameData) {

    var newGameData = {
        "InvestigatorGroupUserId": gameData.GroupUserId,
        "InvestigatorGroupGameId": gameData.GroupGameId,
        "WithTeacher": gameData.WithTeacher
    };

    return $http.post("/APGame.WebHost/play/newGamePlayed", newGameData);
}

原因是函数没有返回任何未定义的值

$http.post("/APGame.WebHost/play/newGamePlayed", newGameData)
    .then(function(response) {
        // notice that you are returning this value to the function(response) not your saveNewGame function
        return response.data;
    });
由于javascript的异步特性,您应该改为执行以下操作$http.post返回一个promise对象,可以像下面这样使用

return $http.post("/APGame.WebHost/play/newGamePlayed", newGameData);
在你的调用函数中

saveNewGame($http, gameData).then(function(response){
    gamePlayedId = response.data;
});

我之前也尝试过类似的方法(现在也尝试过),但我得到了错误:
无法读取未定义的属性“then”
可能是因为您没有返回$http.get承诺。你能试试吗?我刚刚试过你编辑的答案,但把你的提醒改成了
提醒(response.data)
,效果很好,谢谢!。为什么我必须将函数调用中的数据返回到
saveNewGame
而不是函数本身?您必须返回$http承诺才能看到它的解决方案。问题是,当我在函数调用中对
response.data
发出警报时,我得到了正确的值。但是,当我尝试返回
response.data
并在
gameplayedd
上发出警报时,它返回的是一个对象(不包含该值)。当我在函数中对
gameplayedd
发出警报时,我需要能够在
gameplayedd
中访问/存储返回值,然后它返回正确的值。但是,当我在外部执行此操作时,它会显示
未定义
,因为http.post是异步的,所以在http请求完成时会调用函数(响应)。当您在函数外部发出警报时,请求仍未完成,且尚未分配值。