如何在AngularJS中根据对象属性进行过滤?

4

我有两个对象数组,如下所示:

$scope.countries = [
        {'id' : 1, 'country_name':'India'},
        {'id' : 10, 'country_name':'Australia'},
        {'id' : 2, 'country_name':'England'}
    ];
$scope.states = [
        {'state_id' : 1, 'state_name':'Delhi', 'country_id':{'id':1 ,'country_name':'India'}},
        {'state_id' : 5, 'state_name':'London', 'country_id':{'id':2 ,'country_name':'England'}},
        {'state_id' : 2, 'state_name':'Victoria', 'country_id':{'id':10 ,'country_name':'Australia'}}
    ];

我正在尝试根据以下所示的country_id对象内部的id筛选状态:

<select ng-model="sellerCountry" ng-options="item.id as item.country_name for item in countries" required>
    <option value="">Select Country</option>
</select>   

<select ng-model="sellerState" ng-options="item.id as item.state_name for item in states | filter : {country_id: { id : sellerCountry}, }" ng-disabled="!sellerCountry">
    <option value="">Select State</option>
</select>

这里,当我从第一个选择框中选择印度(其中id = 1)时,我应该只能在第二个选择框中显示德里,但它显示德里(country_id.id = 1)和维多利亚(country_id.id = 10)。需要帮助解决这个问题。提前致谢。
1个回答

1
默认情况下,过滤器执行包含搜索,您应该使该过滤器执行严格比较,通过将第三个参数传递为true进行过滤。
 ng-options="item.id as item.state_name for item in states
               | filter : {country_id: { id : sellerCountry} }: true"

演示Plunker


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接