2016-03-05 82 views
-1

以下是我的JSON數據。如何根據類別獲取subCategories angularjs

JSON

{ 
"1":{"id":"1","name":"Websites IT & Software","sub_cat":[ 
    {"id":"1","name":"Build a Website","description":"Build a Website"}, 
    {"id":"2","name":"Build an Online Store","description":"Build an Online Store"}, 
    {"id":"3","name":"Get Traffic to my Website ","description":"Get Traffic to my Website "}, 
    {"id":"4","name":"Write some Software","description":"Write some Software"}, 
    {"id":"5","name":"Convert a Template to a Website","description":"Convert a Template to a Website"}, 
    {"id":"53","name":"Create a Wordpress Template","description":"Create a Wordpress Template"}, 
    {"id":"54","name":"Create a Joomla Template","description":"Create a Joomla Template"}, 
    {"id":"55","name":"Create a Drupal Template","description":"Create a Drupal Template"}, 
    {"id":"56","name":"Develop a Mac Application","description":"Develop a Mac Application"} 
    ]}, 
"2":{"id":"2","name":"Mobile","sub_cat":[ 
    {"id":"6","name":"Write an iPhone application","description":"Write an iPhone application"}, 
    {"id":"7","name":"Write an iPad application","description":"Write an iPad application"}, 
    {"id":"8","name":"Write a Blackberry application","description":"Write a Blackberry application"}, 
    {"id":"9","name":"Write an Android application","description":"Write an Android application"}, 
    {"id":"57","name":"Create a Mobile Website","description":"Create a Mobile Website"}]}} 

所有我想要的是一個基於用戶選擇,我想實現的角度的方式同樣的事情類別獲取子類別。我是否需要再次創建一個Ajax以基於類別獲取子類別?

任何想法,我該如何做到這一點,將不勝感激。謝謝:)

+0

你的意思是:如何訪問JSON鑰匙送過來AJAX? –

+1

瀏覽角度文檔網站上的教程。將向您展示幾種可能的解決方案問題太廣了 – charlietfl

+0

@DiegoGallegos不,我想取取sub_cat取決於類別用戶選擇 –

回答

1

所有你需要從對象中刪除"1""2"鍵,並只是第一對象陣列如下:

$scope.categories = [{ 
     "id": "1", 
     "name": "Websites IT & Software", 
     "sub_cat": [{ 
     "id": "1", 
     "name": "Build a Website", 
     "description": "Build a Website" 
     }, { 
     "id": "2", 
     "name": "Build an Online Store", 
     "description": "Build an Online Store" 
     }] 
    }, 
    { 
     "id": "2", 
     "name": "Mobile", 
     "sub_cat": [{ 
     "id": "6", 
     "name": "Write an iPhone application", 
     "description": "Write an iPhone application" 
     }, { 
     "id": "7", 
     "name": "Write an iPad application", 
     "description": "Write an iPad application" 
     }] 
    }]; 

然後您需要使用ng-optionsng-repeat指令來顯示你所需要的。作爲例子:

<select ng-options="item as item.name for item in categories" ng-model="selectedCategory"> 
</select> 

    <table class="table"> 
     <thead><tr> 
      <th>ID</th> 
      <th>Name</th> 
      <th>Description</th> 
     </tr></thead> 
     <tbody> 
     <tr ng-repeat="subCategory in selectedCategory.sub_cat"> 
      <td>{{subCategory.id}}</td> 
      <td>{{subCategory.name}}</td> 
      <td>{{subCategory.description}}</td> 
     </tr> 
     </tbody> 
    </table> 


Plunkr可供選擇: http://plnkr.co/edit/me0vis?p=preview

+1

非常感謝你的工作:D –

+0

我想改變名稱的類別名稱<選項標籤=「移動」值=「對象:5「>手機 –

+0

在'ng-options'中使用'track by item.name'。已經回答了你的問題,但也留在這裏@ChadidiAbdellah – doroshko

-1

您可以嘗試使用名爲underscore.js的第三方庫。它添加了許多有用的函數,如「where」:

_.where(list, properties)查看列表中的每個值,返回包含屬性中列出的所有鍵值對的所有值的數組。

_.where(listOfPlays, {author: "Shakespeare", year: 1611}); => [{title: "Cymbeline", author: "Shakespeare", year: 1611}, {title: "The Tempest", author: "Shakespeare", year: 1611}] 

這裏是圖書館的頁面的鏈接http://underscorejs.org/#where

您還可以使用lodash:https://lodash.com/