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

Php 如何使用AngularJS在HTML select中显示json响应,php,html,angularjs,json,Php,Html,Angularjs,Json,Json响应 [{"parentCategoryName":"Automobiles","subCategories":[[{"subCategoryID":1,"subCategoryName":"Car Parts & Accessories"},{"subCategoryID":2,"subCategoryName":"Campervans & Caravans"},{"subCategoryID":3,"subCategoryName":"Motorbikes &

Json响应

[{"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"}]]}]
<select class="form-control" ng-model="tipost" ng-options="obj.value group by obj.category for obj in ParentCategory"></select>
答案应为请参见下图

 <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>

HTML代码

 <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>

谢谢

首先将JSON响应保存在变量中

$scope.variable_name = $json_response;
现在在视图中,如果您编写
{{variable_name}}
,它将向您显示JSON响应

使用
ng repeat
在视图中显示为多个选项。检查是否有
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>

您可以使用ng repeat指令,如下所示

<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>

选择一个类别
{{subCategory.subCategory name}}
大家好,我找到了答案

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

            }); 
        }
$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);
                }); 
            }); 
        });
    };
AngularJS控制器

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

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

[{"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"}]]}]
<select class="form-control" ng-model="tipost" ng-options="obj.value group by obj.category for obj in ParentCategory"></select>


我们不是来帮你工作的。至少你必须证明你已经试过了。兄弟,我很累,但不起作用:(@SalihMohamed根据你给出的图像,我相信你应该看看。看看“分组依据”,“分组筛选” features@RaviTeja thnx brong:仅重复显示父类别,我需要显示父类别。在父子类别之后,请查看上图。thnx bro我同意您的结构,我对代码进行了一点更改,最后我得到了确切的答案:)在响应中,我首先得到了多维json数组,您需要将它们转换为单个数组,类似于@Bijay Rai post。