从Angular forEach内部访问外部

时间:2015-09-27 06:24:45

标签: javascript angularjs

我要做的是获取游戏APIService.postData创建的游戏ID。然后我需要获取该游戏ID并将其放入Angular foreach循环中,以便在RESTful端,外键约束成立。

如何从那里获取游戏ID? 附:我很清楚范围问题

this.createGame = function() {

    APIService.postData('game', '', $scope.newGameData).then(function (data) {
        $scope.newGameID = data.id;
    });

    // Looping through each added class and adding the game_id onto the object in
    // order for the DB insertion to go smoothly on the RESTful side.
    angular.forEach($scope.newGameData.classes, function (key, value) {
        $scope.newGameData.classes[value].game_id = $scope.newGameID;
        APIService.postData('game-class', '', $scope.newGameData.classes[value]);
    });

    // Looping through each added race and pushing the game_id onto the object in
    // order for the DB insertion to go smoothly on the RESTful side.
    angular.forEach($scope.newGameData.races, function (key, value) {
        $scope.newGameData.races[value].game_id = $scope.newGameID;
        APIService.postData('game-race', '', $scope.newGameData.races[value]);
    });

    $scope.newGameData = {
        name: ""
    };
    $scope.race_counter = 0;
    $scope.class_counter = 0;
    $scope.newGameData.classes = [{id: $scope.class_counter}];
    $scope.newGameData.races = [{id: $scope.race_counter}];

    $scope.successMessage = "New 'Game' has been added!";

    $scope.action = 'showGames'; // Default action.
    this.getGames();
    $window.scrollTo(0, 0);
};

1 个答案:

答案 0 :(得分:0)

想出来。 foreach循环需要进入'然后'打回来。由于POST请求尚未实际完成,因此GameID一直未定义。最重要的是,$ scope.newGameData被搞砸了,所以我将两个数组分配给它们自己的局部变量,它运行良好。

this.createGame = function() {

    var newGameID = '';
    var classData = $scope.newGameData.classes;
    var raceData = $scope.newGameData.races;

    APIService.postData('game', '', $scope.newGameData).then(function (data) {
        newGameID = data.id;

        // Looping through each added class and adding the game_id onto the object in
        // order for the DB insertion to go smoothly on the RESTful side.
        angular.forEach(classData, function (key, value) {
            classData[value].game_id = newGameID;
            APIService.postData('game-class', '', classData[value]);
        });

        // Looping through each added race and pushing the game_id onto the object in
        // order for the DB insertion to go smoothly on the RESTful side.
        angular.forEach($scope.newGameData.races, function (key, value) {
            raceData[value].game_id = newGameID;
            APIService.postData('game-race', '', raceData[value]);
        });
    });

    $scope.newGameData = {
        name: ""
    };
    $scope.race_counter = 0;
    $scope.class_counter = 0;
    $scope.newGameData.classes = [{id: $scope.class_counter}];
    $scope.newGameData.races = [{id: $scope.race_counter}];

    $scope.successMessage = "New 'Game' has been added!";

    $scope.action = 'showGames'; // Default action.
    this.getGames();
    $window.scrollTo(0, 0);
};