使用选择清除 AngularJS 中的过滤器



我在下面给出的HTML中使用ngOptions指令:

<select ng-model="selectedGroupToShow.Id" ng-options="g.Id as g.Name for g in myGroups">
    <option value>-- All --</option>
</select>

我在 ng-repeat 中用于显示我的数据的过滤器如下所示:

filter:{GroupId:selectedGroupToShow.Id}"

即使我使用

filter:selectedGroupToShow

问题保持不变。重置过滤器..

这非常有效,但是当我想清除过滤器(选择默认选项 - "全部")时,它只会显示根本没有设置 GroupId 参数的数据。

在选择中选择默认选项元素时,如何显示所有数据(清除过滤器)?

我想我理解你的问题,你面临的问题是当下拉列表选择--ALL-时,它的值为空而不是未定义。因此,要重置它,您必须将其设置为未定义。

<div ng-repeat="item in items| filter:{itemId:selectedGroupToShow.Id||undefined}">
         {{item.itemId}}
</div>

请参考我为您的问题创建的以下小提琴。

小提琴

我这样做了,只是用一个空的字符串值。

希望它可以帮助任何人。

翡翠代码:

select.form-control(ng-model="productType", ng-init="productType='free'")
        option(value="free") FREE
        option(value="interest") INTEREST
        option(value="hybrid") HYBRID
        option(value="") ALL
.col-sm-6.col-md-4.col-lg-3(ng-repeat="product in productList | filter:{ type :productType }") {{product.name}} 

最新更新