doulu3865 2016-09-29 07:08
浏览 127
已采纳

如何使用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

  • 写回答

4条回答 默认 最新

  • dsgdg46465 2016-09-29 11:47
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧