我正在尝试将 api 中的数据显示到下拉列表中(部门全名)。我可以显示一个值,但不能显示多个值。你能告诉我哪里出错了吗?
我想从 json 数组中获取值到选择下拉列表中。我想要进入下拉列表的值是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
最佳答案
你可以做到这一点,
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
关于javascript - 如何将不同数组中的多个值添加到下拉列表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41329625/