0

I am trying to display data from an api into a dropdown (Department Full Name). I can display one value but not multiple values. Can you please tell me where am I going wrong?

From the json array I want to get values into select dropdown. The values I want to get into dropdown are shortName, semester and section. I can display only shortName but not other 2 values.

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 array

    {
 "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
        }
    ]

Here is the plnkr

2
  • in your plunker which dropdown you are referring to? Commented Dec 26, 2016 at 10:15
  • @Sajeetharan oh sorry. I am referring to Departname Full name. Commented Dec 26, 2016 at 10:18

1 Answer 1

2

You can do this,

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);
            })

         });

      }
    ]);

EDIT

  response.data.department.sections.forEach(function(key){
           $scope.displayValues.push(response.data.department.shortName + " "+ key.section + " "+key.semester);

     })

DEMO

Sign up to request clarification or add additional context in comments.

7 Comments

Hey thanks for the efforts. How do I to print it in one line like CS C 4
Upvote. This is what I got in my mind after reading the question.
@HebleV did it help?
@Sajeetharan I see that its working plnkr but when i included it does not display.neither it gives any errors
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.