2017-10-21 73 views
0

的陣列NG選項我的數據看起來如下:AngularJS選擇與對象

{ 
    "count": 0, 
    "QHPIssuerNames": [ 
     "NOV BCBS", 
     "mark issuer", 
     "Release 3 issuer", 
     "Los angles issuer" 
    ], 
    "QDPIssuerNames": [ 
     New One", 
     "jan issuer", 
     "Manage Issuer", 
     "test enrollment", 
     "testing issuer p" 
    ] 
} 

而且我想一個選擇添加到我的HTML加載從陣列中的數據。

<select 
    name="issuer" 
    id="issuer" 
    ng-init="vm.selectedData = vm.inputData.issuer[0]" 
    class="form-control" 
    ng-model="vm.inputData.issuer" 
    ng-options="label group by group for value in vm.inputData.issuer"></select> 

Here is the screenshot of how my dropdown should look like

jsfiddle

回答

0

我的解決方案涉及改造object of arrays成和array of objects,我試圖創建一個與原始對象的解決方案,但問題是,我不能夠創建多級選擇使用該對象。它需要將對象分割成單獨的元素以獲得所需的結果,基本上我循環遍歷原始數組並刪除第一個條目,即count:0,因爲它在迭代過程中不需要,然後將值分配給最終陣列。

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

 
app.controller('MyController', function MyController($scope) { 
 
    vm = this; 
 
    vm.inputData = { 
 
    "count": 0, 
 
    "QHPIssuerNames": [ 
 
     "NOV BCBS", 
 
     "mark issuer", 
 
     "Release 3 issuer", 
 
     "Los angles issuer" 
 
    ], 
 
    "QDPIssuerNames": [ 
 
     "New One", 
 
     "jan issuer", 
 
     "Manage Issuer", 
 
     "test enrollment", 
 
     "testing issuer p" 
 
    ] 
 
    }; 
 
    vm.newArray = vm.inputData; 
 
    delete vm.newArray.count; 
 
    vm.finalArray = []; 
 
    for (var k in vm.newArray) { 
 
    for (var j of vm.newArray[k]) { 
 
     vm.finalArray.push({ 
 
     key: k, 
 
     value: j 
 
     }) 
 
    } 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-controller='MyController as vm' ng-app="myApp"> 
 
    <select name="issuer" id="issuer" class="form-control" ng-init="vm.selectedData = vm.finalArray[0]" ng-model="vm.selectedData" ng-options="x.value group by x.key for x in vm.finalArray"></select> 
 
</div>

+0

我想兩個字符串(「人人享有衛生保健發行人」,「所有的牙科發行人」添加到對象數組的前面,顯示在圖像 – dragonfly

+0

@dragonfly對於您先應該分享你正在使用的完整的json! –

+0

@ Naren Murali - 我得到了json作爲http請求的響應,但字符串(「All Health Issuer」,「All Dental Issuer」)不是響應的一部分我必須將它添加到最終數組中 – dragonfly