根据用户选择过滤ng-options

时间:2014-11-12 13:43:24

标签: angularjs filter ng-options

我有两个选择元素。第一个用于选择国家/地区。 第二个用于选择一个州。

每个州都与一个国家绑定。

国:

[{"code":"SC","name":"Santa Catarina","country":{"code":"BRA","name":"Brazil"}},    {"code":"RR","name":"Roraima","country":{"code":"BRA","name":"Brazil"}},{"code":"AL","name":"Alabama","country":{"code":"USA","name":"United States"}},{"code":"FL","name":"Florida","country":{"code":"USA","name":"United States"}}] 

国家:

[{"code":"BRA","name":"Brasil"},{"code":"USA","name":"United States"}] 

HTML内容:

<select ng-model="userCountry" ng-options="country.name for country in countries track by country.name">

 <select ng-model="userState" ng-options="state.name for state in states track by state.name | filter: {state.country.code:userCountry.code}">

因此,首先,用户选择一个与&#34; userCountry&#34;相关联的国家/地区。

$scope.userCountry = new Object();

现在,状态选择元素应仅显示与该国家/地区相关联的状态,但其当前正在加载所有状态。它不起作用:(

我不知道如果我使用正确的语法。任何人都可以帮助我吗?

这是我的过滤器:

| filter: {state.country.code:userCountry.code}

我尝试了许多不同的变化而没有成功。

这是我的问题的小提琴: http://jsfiddle.net/zrm7y80s/2/

它使用角度1.2.23

谢谢!

1 个答案:

答案 0 :(得分:6)

将ng-options更改为:

<select ng-model="userState" ng-options="state.name for state in ( states | filter: {country: { code: userCountry.code }}) track by state.name">

您希望在执行跟踪之前运行过滤器,也希望过滤子对象,因为state.country.code作为对象键无效。

更新小提琴:http://jsfiddle.net/dw20jakt/

相关问题