使用angularjs中的dropdown1填充下拉列表2

时间:2014-11-12 08:03:07

标签: javascript json angularjs angularjs-ng-repeat

我的HTML:

<select ng-controller="category"  ng-model="selectedTestAccount" ng-options="c.category for c in categories track by c.categoryID" ></select>
<select ng-controller="subcatgory"  ng-model="selectedTestAccount1" ng-options="c.subcategory for c in subcategories track by c.subcategoryID"></select>

我的json看起来像:

json1:

category: "Restaurants"categoryID: "1"

json2:

category: "Restaurants"categoryID: "1"subcategory: "European"subcategoryID: "1"
category: "Restaurants"categoryID: "1"subcategory: "Food Carts"subcategoryID: "17"

我想要创建两个下拉菜单。一个用于显示类别的第一个json。 选择第一个类别我想要从第二个json列出子类别。如何将第一次下拉列为强制性。     任何人都可以帮助这个

3 个答案:

答案 0 :(得分:0)

你只需要:

  1. 在第一个选择
  2. 上添加required属性
  3. 在select中放入一个空的<option></option>(否则select已经有一个满足要求的默认选项)
  4. 以下是这个例子: http://jsfiddle.net/n5568q6o/1/

答案 1 :(得分:0)

您可以使用手表观看第一个物体。如果更改,则更改第二个数组。 这将根据您想要的选项更改选项。

答案 2 :(得分:0)

所以我创建了一个plnker

键是$watch,它查看第一个列表的选定值(ng-model),然后根据该新值更改第二个列表的值。

$scope.$watch('selectedCategory', function(newValue) {
  for (var i = 0; i < $scope.subCategories.length; i++){
    if ($scope.subCategories[i].name === newValue){
      $scope.selectedSubCategoryValues = $scope.subCategories[i].values;
    }
  }
});