2016-07-27 105 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) 
) 
相關問題