我正在尝试将api中的数据显示为下拉列表(部门全名)。我可以显示一个值但不能显示多个值。你能告诉我哪里出错了吗?
从json数组我希望将值输入到select下拉列表中。我想要进入下拉列表的值是 shortName ,学期和部分。我只能显示shortName而不能显示其他2个值。
index.controller.js
$http.get('https://student.herokuapp.com/department/')
.success(function(response){
$scope.deptDetails=response.department;
// console.log($scope.deptDetails);
deptLen=response.department.sections.length;
console.log(deptLen);
for(var i=0;i<deptLen;i++){
$scope.deptDetails.push({"_id":response.department._id,
"shortName":response.department.shortName,
"section":response.department.sections[i].section,
"semester":response.department.sections[i].semester});
}
});
Json数组
{
"department": {
"_id": "585955afce1a5e0011a2ecf5",
"collegeId": "58295efb836f8611d5e3e4cb",
"fullName": "Compcsuter Science",
"shortName": "CS",
"numberOfSemesters": "8",
"semesterYearScheme": "Semester",
"programName": "UG",
"numberOfSections": "1",
"sections": [
{
"_id": "585fb137bf29882207752552",
"collegeId": "585e53c6729e5c2214b97a0f",
"departmentId": "585fb137bf29882207752546",
"semester": 4,
"section": "C",
"syllabus": [],
"teachers": [],
"students": [],
"createdBy": "58332b90a7986c09b7e81980",
"createdAt": "2016-12-25T11:44:55.445Z",
"__v": 0
}
]
以下是plnkr
答案 0 :(得分:2)
你可以这样做,
app.controller("dobController", ["$scope", '$http',
function($scope, $http) {
$scope.displayValues = [];
$http.get('test.json').then(function(response){
$scope.data = response.data;
console.log(response.data.department);
$scope.displayValues.push(response.data.department.shortName);
response.data.department.sections.forEach(function(key){
$scope.displayValues.push(key.section);
$scope.displayValues.push(key.semester);
})
});
}
]);
修改强>
response.data.department.sections.forEach(function(key){
$scope.displayValues.push(response.data.department.shortName + " "+ key.section + " "+key.semester);
})
<强> DEMO 强>