如何使用ng-options创建嵌套选择

时间:2015-12-23 08:49:08

标签: angularjs

我想将默认值设置为pcateccate.id,我应该如何或应该修复什么?

我的 HTML 就在这里,

<select 
    ng-model="pcate" 
    ng-required="true" 
    ng-change="show(pcate)" 
    ng-options=" pcate.name for pcate in cate">
</select>

<select 
    ng-model="ccate.id" 
    ng-required="true" 
    ng-change="shows(ccate.id)" 
    ng-options="ccate.id as ccate.name for ccate in pcate.son">
</select>

我的 JSON结构如下。

$scope.cate = [
  {
    "son": [
      {
        "parent_id": 100,
        "cate_line": "",
        "id": 101,
        "name": "food1"
      },
      {
        "parent_id": 100,
        "cate_line": "",
        "id": 102,
        "name": "food2"
      },
      {
        "parent_id": 100,
        "cate_line": "",
        "id": 103,
        "name": "food3"
      },
      {
        "parent_id": 100,
        "cate_line": "",
        "id": 104,
        "name": "food4"
      },
      {
        "parent_id": 100,
        "cate_line": "",
        "id": 105,
        "name": "food5"
      },
      {
        "parent_id": 100,
        "cate_line": "",
        "id": 106,
        "name": "food6"
      }
    ],
    "parent_id": null,
    "cate_line": "",
    "id": 100,
    "name": "food"
  },
  {
    "son": [
      {
        "parent_id": 200,
        "cate_line": "",
        "id": 201,
        "name": "flower1"
      },
      {
        "parent_id": 200,
        "cate_line": "",
        "id": 202,
        "name": "flower2"
      },
      {
        "parent_id": 200,
        "cate_line": "",
        "id": 203,
        "name": "flower3"
      },
      {
        "parent_id": 200,
        "cate_line": "",
        "id": 204,
        "name": "flower4"
      },
      {
        "parent_id": 200,
        "cate_line": "",
        "id": 205,
        "name": "flower5"
      },
      {
        "parent_id": 200,
        "cate_line": "",
        "id": 206,
        "name": "flower6"
      }
    ],
    "parent_id": null,
    "cate_line": "",
    "id": 200,
    "name": "flower"
  },
  {
    "son": [
      {
        "parent_id": 300,
        "cate_line": "",
        "id": 301,
        "name": "service1"
      },
      {
        "parent_id": 300,
        "cate_line": "",
        "id": 302,
        "name": "service2"
      },
      {
        "parent_id": 300,
        "cate_line": "",
        "id": 303,
        "name": "service3"
      },
      {
        "parent_id": 300,
        "cate_line": "",
        "id": 304,
        "name": "service4"
      },
      {
        "parent_id": 300,
        "cate_line": "",
        "id": 305,
        "name": "service5"
      },
      {
        "parent_id": 300,
        "cate_line": "",
        "id": 399,
        "name": "service6"
      }
    ],
    "parent_id": null,
    "cate_line": "",
    "id": 300,
    "name": "service"
  }
]

1 个答案:

答案 0 :(得分:0)

你可以使用这样的东西..

   <select ng-model="selectedItem" ng-options="item as item.Text for item in      items"></select>

此致 Jasbeer Singh