2016-09-29 34 views
-1

JSON响应如何显示在HTML JSON响应选择使用AngularJS

[{"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"}]]}] 

答案应该是请参考下面的图像

I want to display the above json response like this

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> 

AngularJS控制器

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

      }); 
     } 

感谢

+1

我们不是在这里做你的工作。至少你必须展示你的尝试。 – Malkev

+0

兄弟我累了很多,但不工作:( –

+0

@SalihMohamed根据你给的形象,我相信你应该看看http://angular-ui.github.io/ui-select/。看看“Group by”,“Group Filter”features –

回答

0

喜逢身体我找到答案

AngularJS控制器

$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> 
+0

在响应我得到多维json数组首先你需要将它们转换为单数组,这就像@Bijay雷后。 –

0

首先保存在变量

$scope.variable_name = $json_response; 

你的JSON响应现在,在视图如果你写{{variable_name}},它会显示你的JSON响应。

使用ng-repeat显示为多个选项。检查http://www.w3schools.com/angular/ng_ng-repeat.aspng-repeat

+0

ng:重复只显示父类别,我需要显示父类别,在父子类别请看上面的图像 –

0

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

+0

thnx兄弟我去你的结构和我改变了我的代码的一点,最后我得到了确切的答案:) –

0

可以使用NG-重复指令如下。

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