javascript - 如何在 AngularJS 中按对象的属性进行过滤?

标签 javascript angularjs angular-filters

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

$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>

在这里,当我从第一个选择框中选择 India(其中 id = 1)时,我应该只显示 Delhi在第二次选择中,但它显示德里(country_idid = 1)和维多利亚(country_id .id = 10)

需要帮助来解决这个问题。提前致谢。

最佳答案

默认情况下过滤器执行包含搜索,您应该通过将第三个参数传递给true 来使该过滤器进行严格比较。

 ng-options="item.id as item.state_name for item in states
               | filter : {country_id: { id : sellerCountry} }: true"

Demo Plunker

关于javascript - 如何在 AngularJS 中按对象的属性进行过滤?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46101036/

相关文章:

javascript - 有没有使用 Angular.js 和 Indexed DB 的好方法?

javascript - If 语句在 JavaScript 中不起作用 - HTML

html - 我想对齐出现在中心的 div 标签中的文本,它来自 $scope.Message。但无法对齐它

javascript - AngularJs 过滤器

javascript - Angular 日期过滤器的格式部分不起作用

javascript - 通过 JavaScript 更改 img src 而无需在 HTML 中单击

javascript - Twitter typeahead 0.1 w/bloodhound - 无法使用 URL 进行预取

c# - AngularJS 应用程序可以使用哪些 .NET 服务器端技术?

html - 选择中的 A​​ngularjs ng-init 不起作用

javascript - 如何在数组上使用 ng-repeat 和 filter 属性?