简体   繁体   English

如何在AngularJS中按对象的属性过滤?

[英]How to filter by object's property in AngularJS?

I have two array of objects as shown below: 我有两个对象数组,如下所示:

$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'}}
    ];

I'm trying to filter the states based on the id inside the country_id object as shown below: 我正在尝试根据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>

Here, when i select India ( where id = 1) from the first select box, i should only get Delhi to be displayed in second select, but its displaying Delhi ( country_id . id = 1) and Victoria ( country_id . id = 10 ) 在这里,当我选择印度 (其中id = 1)从第一个选择框,我应该只得到第二选择要显示德里 ,但其显示德里 (COUNTRY_ID。ID = 1)和维多利亚 (COUNTRY_ID。ID = 10)

Need help to sort out this. 需要帮助来解决这个问题。 Thanks in advance. 提前致谢。

By default filters do contains search, you should make that filter to do strict comparison, by passing 3rd parameter to true to filter. 默认情况下,过滤器确实contains搜索,您应该通过将3rd参数传递给true来使过滤器进行严格比较。

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

Demo Plunker 演示柱塞

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM