2016-07-27 111 views
-1

我目前正在尝试使用$ http服务来尝试读取一些JSON文件。我成功地阅读了它们,但出于某种原因出现为object Object,所以我无法读取这些对象的属性。

我是否在使用GET请求时错误地读取文件?但这很奇怪,因为当我在数据上使用JSON.stringify()时,我可以看到应该在那里的所有属性......只是当我正常得到它们时,它们显示为这些未定义的对象。这可能是我初始化变量的方式吗?

这里是我的功能来从JSON文件中的数据:

myMod.service('deployService', function ($http, envService) { 
var projectCache = []; 
var envCache = []; 
var envFilter = ""; 

function getDeployments() { 
    if (!projectCache) { 
     // return; 
    } else { 
     $http({ 
      method: 'GET', 
      url: '/json_files/dashboard.json' 
     }).then(
      function success(response) { 
       this.projectCache = response.data; 
       this.envFilter = envService.envFilter; 

       for (var i = 0; i < response.data.length; i++) { 
        // var item = JSON.stringify(response.data[i].environmentStatuses); 
        var item = response.data[i].environmentStatuses; 

        console.log("THE ITEM IS: " + item); 
        // this.envCache.push(item); 
        envCache.push(item); 

       } 

       console.log(envCache); 
      }, 
      function failure(reason) { 
       console.log("there was a failure in getProject in deployController") 
      }); 
    } 
}; 


return { 
    projectCache: projectCache, 
    envCache: envCache, 
    getDeployments: getDeployments, 
    envFilter: envFilter, 
    clearCaches: clearCaches 
}; 

}); 

这里是控制器的代码:你需要包括你的服务作为一个依赖

myMod.controller("deployController", ['$scope', '$http', '$location', 'envService', 'deployService', function ($scope, $http, $location, envService, deployService) { 

//store the projects in this array 
$scope.projects = []; 
$scope.editingData = {}; 


$scope.selectedRow = []; // initialize our variable to null 
$scope.setClickedRow = function (index) { //function that sets the value of selectedRow to current index 
    $scope.selectedRow[i] = $scope.selectedRow[i] != true; 
    console.log(index); 
    console.log("row " + $scope.selectedRow[i]); 
}; 


//being used to fetch the name and to get the index 
$scope.getProjName = function (index) { 
    // $scope.envFilter = { 
    //  name: "" 
    // }; 
    console.log("INSIDE GETPROJNAEM"); 
    envService.clickedEnv(index, $scope.projects); 

    //console.log($scope.envFilter); 
}; 



$http({ 
    method: 'GET', 
    url: './json_files/dashboard.json' 
}).then(function successCallback(response) { 
    $scope.projects = response.data; 

}, function errorCallback() { 
    alert("cant find the dashboard json"); 
}); 






//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ENVIRONMENT CONTROLLER LOGIC~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
$scope.projEnvironments = []; 
$scope.editingData = {}; 

deployService.getDeployments(); 
$scope.projEnvironments = deployService.envCache; 
$scope.envFilter = deployService.envFilter; 
console.log("DEPLOYSERVICE.ENVCACHE: " + deployService.envCache); 
// console.log("projenv: " + $scope.projEnvironments); 
// console.log("envFilter: " + $scope.envFilter); 


$scope.getBranchName = function (environment) { 
    envService.setBranchName(environment); 
    console.log("THE ENVBRANCH:" + $scope.envBranch); 
}; 

}]); 
+1

您需要从您的Angular控制器调用服务。然后,您将能够阅读像deployService.GetDeployments() –

+0

@RaniRadcliff是的内容。我在我的控制器中调用'deployService.GetDeployments()',但它仍然返回'object Object'。通常,当一个对象返回时,在Chrome开发者工具中有一个下拉菜单,但是这些对象没有那个 – winsticknova

+0

@winsticknova你能用你的控制器代码更新你的文章吗? –

回答

0

您相关控制人。然后,从控制器内的服务中调用适当的方法。

例如:

myApp.controller('aController', ['$scope', 'deployService', function ($scope, deployService) { 
    //Some $scope variables and other controller code 
    deployService.getDeployments(); 
    $scope.myData = deployService.<variable from service>; 
}]); 

此外,为了在JavaScript数组指派变量时要小心;分配将通过引用来完成,而不是通过值!如果没有说明,这可能会产生不希望的结果。

+0

我已经将服务添加到了我的控制器。我在调用服务中的函数时没有任何问题 - 只是GET请求中的返回对象的属性由于某种原因而无法读取。一切似乎都没有定义。 – winsticknova

+0

@winsticknova如果GET请求中的结果不正确,在后端听起来像是一个问题。你在后端使用什么框架或语言? –

0

尝试重新编写代码在你的控制器是这样的:

deployService.getDeployments() 
.success(function(data){ 
$scope.myData = data; 
}) 

如果将鼠标悬停在$scope.myData不告诉你对象的数组,还有别的东西错了。

0

您在$http请求中从服务器返回的实际json对象是response.data

如果你的json包含一个“数据”属性作为数组,你需要遍历它;

response.data.data.forEach(
    d => JSON.stringify(d.environmentStatuses) 
)