【发布时间】:2015-07-23 14:15:58
【问题描述】:
我正在尝试创建一个键和值数组以发送到我的 laravel 后端。
这是我当前的代码
$http.post('../survey/'+$scope.clientID+'/getSurvey', {client: $scope.clientID }).
success(function(data, status, headers, config) {
console.log(data);
$scope.survey_id = data[0];
$scope.questions = data;
//$scope.dLength = data.length;
$scope.dLength = 5;
console.log($scope.questions);
// When an answer button is clicked
$scope.clicky = function(value) {
// Add a class to hide the div cntainng the question
$scope.class = "hideit";
//Set a timeout function so the question can fadeOut before we proces anything
var callAtTimeout = function() {
// Check to see if this is the last question
if($scope.qid >= $scope.dLength -1){
// All questions have been answered. AJAX the data back to Laravel
alert("complete")
$http.post('../survey/'+$scope.survey_id.survey_id+'/submitSurvey', {data: angular.fromJson($scope.answers)}).
success(function(data, status, headers, config) {
console.log(data);
})
//})
//console.log($scope.answers)
}else{
//Build up the Array of answeres
// Get Question name - this will be the key
var questionName = $scope.questions[$scope.qid].name;
// This is the value
var questionAnswer = value
//build the aray element
var line = { questionName : value };
//Push it to the array
$scope.answers.push({questionName: questionAnswer});
//console.log($scope.answers)
//add to the count variable
$scope.qid += 1;
// Animate the question div back up to display the next question
$scope.class = "showit";
}
}
$timeout(callAtTimeout, 1000)
}
}).
当我查看我正在构建的数据时,它显示问题名称作为键,而不是变量问题名称中的数据。
当我警告变量问题名称时,它显示了正确的数据,但数组只是将键显示为“问题名称”,我是否构建了错误的数组,如果是,我应该如何构建它?
【问题讨论】:
标签: php json angularjs laravel