2016-05-11 59 views
0

我想從我的數據集中獲取json對象的名稱(本例中爲bdev0bdev1)。如何獲取json對象的角度名稱

到目前爲止,我已嘗試ng-options沒有成功。我在下面的代碼中試圖實現的只是檢索json對象的名稱。到目前爲止,我所嘗試過的所有例子都不適用於我的案例。

jsfiddle

代碼:

var myApp = angular.module('myApp', []); 

//myApp.directive('myDirective', function() {}); 
//myApp.factory('myService', function() {}); 

function MyCtrl($scope) { 
    $scope.data = [{ 
     "Block_Devices": { 
      "bdev0": { 
       "Backend_Device_Path": "/dev/ram1", 
       "Capacity": "16777216", 
       "Bytes_Written": 1577, 
       "timestamp": "4365093970", 
       "IO_Operations": 17757, 
       "Guest_Device_Name": "vdb", 
       "Bytes_Read": 17793, 
       "Guest_IP_Address": "192.168.26.88" 
      }, 
      "bdev1": { 
       "Backend_Device_Path": "/dev/ram2", 
       "Capacity": "16777216", 
       "Bytes_Written": 1975, 
       "timestamp": "9365093970", 
       "IO_Operations": 21380, 
       "Guest_Device_Name": "vdb", 
       "Bytes_Read": 20424, 
       "Guest_IP_Address": "192.168.26.100" 
      } 
     }, 
     "Number of Devices": 2 
    }] 

} 

HTML

<div ng-controller="MyCtrl"> 
    <select name="Block_Devices" ng-model="selectedDevice" ng-options="value.Block_Devices for value in data"> 
    <option value="">Please Select Device</option> 
    </select> 
</div> 
Selected Device = {{selectedDevice}} 

回答

1

您可以使用ng-repeat="(key, value) in array"語法。

<div ng-repeat="(key, value) in data[0].Block_Devices"> 
{{key}} 
</div> 

{{鍵}}將Block_Devices的財產
看樣jsfiddle

+0

NG選項=」數據[0]中的(鍵,值)鍵.Block_Devices「將在select中工作 – Nils

1

由於盂蘭盆我面前說你可以做這個jsfiddle

<select name="Block_Devices" ng-model="selectedDevice.Bytes_Written" ng-options="key for (key, value) in data[0].Block_Devices"> 
    <option value="">Please Select Device</option> 
    </select>