1

I have a json file as

var myJson = {
   "Number of Devices":2,
   "Block Devices":{
      "bdev0":{
         "Backend_Device_Path":"/dev/ram1",
         "Capacity":"16777216",
         "Bytes_Written":9848,
         "timestamp":"4365093970",
         "IO_Operations":87204,
         "Guest_Device_Name":"vdb",
         "Bytes_Read":107619,
         "Guest_IP_Address":"192.168.26.88"
      },
      "bdev1":{
         "Backend_Device_Path":"/dev/ram2",
         "Capacity":"16777216",
         "Bytes_Written":10062,
         "timestamp":"9365093970",
         "IO_Operations":93789,
         "Guest_Device_Name":"vdb",
         "Bytes_Read":116524,
         "Guest_IP_Address":"192.168.26.100"
      }
   }
}

I want to push names of block devices in a dropdown menu. the names are bdev0 and bdev1 after looking at some example I tried the following code but I just returns [Object object] in the drop down

view

<select ng-model="selectedDevice" ng-options="item as devices['Block Devices'] for item in selectdevice">
    <option value="">Select Device</option>
   </select>

And controller

    .controller('timeSeriesCtrl', function($scope, $timeout, $http) {

    $scope.selectedDevice = null;
    $scope.selectdevice = [];
    $scope.bytesRead = [];
    $scope.bytesWritten = [];
    $scope.IoOps = [];
    $scope.currentTime = [];
    var path = 'http://orbit5.ds.cs.umu.se:8888/vrio/debug/blk'


function getData(){
  $http.get(path)
  .success(function(data){
    $scope.devices = data
    angular.forEach($scope.devices['Block Devices'], function(value, key){

           $scope.selectdevice = key;
})


  })
}

getData();





  });
3
  • You want ng-options="item.displayName as item for item in devices['Block Devices']" Commented Feb 10, 2016 at 13:21
  • it still just returns [Object object] Commented Feb 10, 2016 at 13:22
  • Note: please use services for making HTTP requests. Commented Feb 10, 2016 at 13:29

2 Answers 2

2

Remove all of the code for the forEach and just use the (key,value) syntax for ng-options

function getData(){
  $http.get(path)
  .success(function(data){
    $scope.devices = data
    //angular.forEach($scope.devices['Block Devices'], function(value, key){
       //$scope.selectdevice = key;
    //   value.key = key;
    //})
  });

In html:

<select ng-model="selectedDevice" 
      ng-options="item as key  for (key,item) in devices['Block Devices'] ">
    <option value="">Select Device</option>
</select>

Fiddle: https://jsfiddle.net/ncapito/4mqFJ/6/

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

6 Comments

Gives me error Error: [$parse:syntax] Syntax Error: Token 'as' is an unexpected token at column 10 of the expression [item.key as devices['Block Devices']] starting at [as devices['Block Devices']].
So i updated it i didn't even see how you were setting devices. I'm assuming i'm showing the right set.
Sorry about that Just updated the code you can see now how I set devices
I think i know what you want to do and I simplified it dramatically.
your original answer did not had (key, value) trick. Its got updated after I posted my answer. I verified this just now.
|
1

object.toString() = [object object]. Object.prototype.toString()

You can use the (key, content) syntax to display keys.

<select ng-model="selectedItem">
      <option ng-repeat="(key, content) in myjson['Block Devices']" value="{{item}}">{{key}}</option>
</select>

1 Comment

No need for ng-repeat you can just use ng-options to do the same.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.