如何使用AngularJS在HTML select中显示json响应

Json Response

[{"parentCategoryName":"Automobiles","subCategories":[[{"subCategoryID":1,"subCategoryName":"Car Parts & Accessories"},{"subCategoryID":2,"subCategoryName":"Campervans & Caravans"},{"subCategoryID":3,"subCategoryName":"Motorbikes & Scooters"},{"subCategoryID":4,"subCategoryName":"Motorbike Parts & Accessories"},{"subCategoryID":5,"subCategoryName":"Vans, Trucks & Plant"},{"subCategoryID":6,"subCategoryName":"Wanted"}]]}]

Answer should be Please see below image

I want to display the above json response like this

HTML Code

 <select name="category-group" id="category-group" class="form-control">
        <option value="0" selected="selected"> Select a category...</option>
        <option value="Vehicles" style="background-color:#E9E9E9;font-weight:bold;" disabled="disabled"> - Vehicles -
        </option>
        <option value="Cars"> Cars</option>
        <option value="Commercial vehicles"> Commercial vehicles</option>
        <option value="Motorcycles"> Motorcycles</option>
        <option value="Motorcycle Equipment"> Car &amp; Motorcycle</option>
    </select>

AngularJS Controller

$scope.getCategory = function() {
            ApiService.getCategory().then(function (response) {
                $scope.categoriesBody = response;   
                console.log($scope.categoriesBody);

            }); 
        }

Thanks

Hi every body i found answer

AngularJS Controller

$scope.getCategory = function() {
        ApiService.getCategory().then(function (response) {
            $scope.categoriesBody = response;
            $scope.parentArray=[];
            $scope.subArray=[];
            $scope.ParentCategory = [];
            angular.forEach($scope.categoriesBody, function(parentCat){
                $scope.parentArray = {'category': parentCat.parentCategoryName, 'value' : []};
                angular.forEach(parentCat.subCategories[0], function(subCat){
                    $scope.subArray = {'category': parentCat.parentCategoryName, 'value' : subCat.subCategoryName}; 
                    $scope.ParentCategory.push($scope.subArray);
                }); 
            }); 
        });
    };

HTML

<select class="form-control" ng-model="tipost" ng-options="obj.value group by obj.category for obj in ParentCategory"></select>

First save your JSON response in a variable

$scope.variable_name = $json_response;

Now in view if you write {{variable_name}} , it will display you the JSON response.

Use ng-repeat to show as multiple options in view. Check http://www.w3schools.com/angular/ng_ng-repeat.asp for ng-repeat

HTML

    <div ng-app="app">
 <div ng-controller="ParentCtrl">
     <select ng-model="tipost" 
        ng-options="obj.value group by obj.category for obj in accessory"><br>
</select>
 </div>
</div>

javascript

angular.module('app', [])

function ParentCtrl($scope){
       $scope.accessory = [
             {"ID":"1", "category":"Vehicles", "value":"cars"},
             {"ID":"2", "category":"Vehicles", "value":"Comercial vehicles"},
             {"ID":"3", "category":"Vehicles", "value":"Boat"},
             {"ID":"4", "category":"Vehicles", "value":" motorcycle"},                  {"ID":"5", "category":"Vehicles", "value":"other"},
             {"ID":"6", "category":"House and children", "value":"Appliances"},
             {"ID":"7", "category":"House and children", "value":"inside"},
             {"ID":"8", "category":"House and children", "value":"game n clothing"},
             {"ID":"9", "category":"House and children", "value":"garden"},
             {"ID":"10", "category":"House and children", "value":"other"},
             {"ID":"11", "category":"Multimedia", "value":"telephony"},
             {"ID":"12", "category":"Multimedia", "value":"image and sound"},
        ];
}

working Jsfiddle link

You can use ng-repeat directive as following.

<select ng-model="car">
   <option value="">Select a category</option>
   <optgroup ng-repeat="cGroup in categoriesBody" label="{{cGroup.parentCategoryName}}">
       <option ng-repeat="subCategory in cGroup.subCategories" value="subCategory.subCategoryID">
              {{subCategory.subCategoryName}}
       </option>
   </optgroup>
</select>