如何使用 angularjs 更改另一个 select 标签的值?

How to use angularjs to change the values of another select tag?

我有两个 Web API 正在呼叫。第一个 api 类型中的每个值都有一个 ID 需要用于 filter 第二个 api 即类别。 JSON 如下:

// Types
[{"ID":1,"Text":"Hardware","Description":"Hardware"}, 
 {"ID":2,"Text":"Software,"Description":"Software"}"}];

// Categories
[{"ID":1,"TypeID":1,"ParentID":0,"Name":"Printing"}, 
 {"ID":2,"TypeID":1,"ParentID":1,"Name":"Toner"}]

首先,您必须存储将用于过滤另一个数组的 selectedTypeID,然后您可以在第二个数组中调用 Angular $filter

语法:

$filter('filter')(array, expression, comparator)

例如:

$filter('filter')($scope.categories, {TypeID: $scope.selectedTypeID}, comparator)

我想你是在问这个:*我如何根据另一个 select 输入中的选择动态填充 select 输入?

我认为你可以走两条路。如果总数据不多(比如 Categories),您可以在第二组数据上使用 filterhttps://docs.angularjs.org/api/ng/filter/filter#!

您可以在 HTML 或控制器中执行此操作!

<!-- HTML Page -->
<select>
  <option ng-repeat="value in values | filter..." value=value>
</select>

// controller.js
values = ($filter('filter')(result.Values, {Id: $routeParams.Id}));

如果要过滤的数据很多,请考虑延迟对 Category API 的调用,直到选择了 Type

没有看到 controller/HTML 之类的东西,很难给出具体的答案,但我认为这会让你朝着正确的方向前进。

$scope.countries = CustomerService.getCountry();

$scope.getCountryStates = function(){
    $scope.sates = CustomerService.getCountryState($scope.customer.Country);
    $scope.cities =[];
};

<select ng-model="customer.State" 
    ng-options="x.Id as x.state for x in sates"
    ng-change = "getStateCities()"
    class="form-control"
    ng-required="true"
    id="state">
    <option value="">-- Choose State --</option>
</select> 

<select  ng-model="customer.City" 
  ng-options="x.Id as x.city for x in cities"
    class="form-control" 
    ng-required="true"
    id="city">
    <option value="">-- Choose City --</option>
</select>

Check complete code with demo