从 angularJS 服务返回的对象的格式很奇怪

Returned object from angularJS service is oddly formed

我在Angular有服务:

  /* App Module */
var triviaApp = angular.module('triviaApp', ['ngRoute', 'ngResource', 'ngCookies','ngAnimate', 'ui.bootstrap']);
  
  triviaApp.service('GameService', ['$rootScope', '$log', '$http', '$resource', '$location', function($rootScope, $log, $http, $resource, $location) {
    
     this.newGame = function(playerId, aiLevel, opponentId) {
  console.log('newGame - init');
  
  return $http({
   url: triviaAppConfig.rootAPIUrl + 'Players/' + playerId + '/Games?aiLevel=' + aiLevel + '&OpponentId=' + opponentId,
   method: "POST",
   })
   .then(function(response) {    
    return response.data;
   }, function(response) {
    $log.warn('Error in $http - getGames in controller...');
  });
 
 } 
    
    }]);

这是调用我的服务并将其附加到 newGameBtn 指令的控制器....

  // HOME CONTROLLER
  triviaApp.controller('HomeController', ['$rootScope', '$scope', '$http', '$cookies', 'GameService', '$log',
    function($rootScope, $scope, $http, $cookies, GameService, $log ) {

            // Initiate New Game Function
        $scope.newGameBtn = function(emailId, aiLevel, opponentId) {
        $scope.gameObj = GameService.newGame(emailId, aiLevel, opponentId);
            console.log($scope.gameObj);

        }


]);


</script>
    <div class="col-md-6 new-game-button">
        <a href="" ng-click="newGameBtn(emailId, 'none', 0)">
            <img src="/img/home/player-vs-player.png" width="256" height="161" alt="Player Vs Player">
            <p>New auto-matched Game</p>
        </a>
    </div>

我 运行 遇到的问题是 $scope.gameObj 回归为:

d {$$state: Object}
$$state: Object
status: 1
value: Object
ActivePlayer: 40
ConsecutiveAnswerCount: 0
ConsecutiveAwardsCount: 0
EndTime: "0001-01-01T00:00:00"
GameId: 1168
GameRound: 0
IsAwardRound: false
IsEndOfTurnRound: false
IsGameOverRound: false
Player1: Object
Player1AnswerSeconds: 0
Player1Awards: Array[0]
Player1Score: 0
Player2: Object
Player2AnswerSeconds: 0
Player2Awards: Array[0]
Player2Score: 0
StartTime: "2015-09-28T15:45:09.5246982Z"
Status: "InProgress"
WinningPlayer: 0
__proto__: Object
__proto__: Object
__proto__: d

现在,从周围的阅读中听起来我得到了承诺而不是对象,但我的问题是如何重写服务和控制器以将 $scope.gameObj 设置为我的对象数据而不是承诺?

做出服务return承诺:

this.newGame = function(playerId, aiLevel, opponentId) {
  return $http({
    url: triviaAppConfig.rootAPIUrl + 'Players/' + playerId + '/Games?aiLevel=' + aiLevel + '&OpponentId=' + opponentId,
    method: "POST",
  });
} 

并让您的控制器等待其解决:

GameService.newGame(emailId, aiLevel, opponentId).then(function(response) {
  $scope.gameObj = response.data;
});