2016-09-29 33 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>