2016-12-26 74 views
0

我正在嘗試將api的數據顯示到下拉列表中(部門全名)。我可以顯示一個值,但不能顯示多個值。你能告訴我我哪裏錯了嗎?如何將不同數組中的多個值添加到下拉菜單中?

從json數組中,我想獲得值選擇下拉列表。我想進入下拉列表的數值是:,簡稱,學期部分。我只能顯示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

在plunker其下拉菜單,你指的是? – Sajeetharan

+0

@Sajeetharan對不起。我指的是出發件全名。 – HebleV

回答

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

+0

嘿,謝謝你的努力。我如何在一行打印如CS C 4 – HebleV

+0

@HebleV檢查此https://plnkr.co/edit/SkzfOqyZ1frxtamrBfQJ?p=preview – Sajeetharan

+0

Upvote。這是我看完這個問題後腦海里浮現的東西。 –

相關問題