我目前正在尝试使用$ 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);
};
}]);
答案 0 :(得分: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中为变量分配变量时要小心;作业将通过参考而非价值来完成!如果不加以考虑,这可能会产生不希望的结果。
答案 1 :(得分:0)
尝试在控制器中重写代码,如下所示:
deployService.getDeployments()
.success(function(data){
$scope.myData = data;
})
如果将鼠标悬停在$scope.myData
上并没有显示对象数组,则会出现其他问题。
答案 2 :(得分:0)
$http
请求中从服务器返回的实际json对象为response.data
。
如果您的json包含&#34;数据&#34;如数组属性,你需要迭代它作为;
response.data.data.forEach(
d => JSON.stringify(d.environmentStatuses)
)