javascript - 如何将不同数组中的多个值添加到下拉列表中?

标签 javascript angularjs dropdown

我正在尝试将 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/

相关文章:

javascript - 防止 AngularJS 删除数据输入属性

flutter - 删除文本和DropdownButton的预告片图标之间的空白区域

javascript - 下拉 Bootstrap 行为很奇怪

javascript - 汇总构建后 : regeneratorRuntime is not defined

javascript - 如何迭代彼此独立的列表组

javascript - 使用 javascript 滑动 html 部分的最佳方法是什么?

javascript - 将最大字符长度放在 Angular 表达式上并添加省略号

javascript - 在 Angular.js 中动态加载模板(部分)

html - 移动设备上不显示滚动条

php - jquery联系表单php问题