我遇到了一个我想弄清楚的自定义过滤器的问题。我已经配置了jsFiddle to replicate the issue。
缺货过滤器是具有奇怪行为的自定义过滤器。最初它工作正常。但是,如果您在搜索框中输入文本,从搜索框中删除文本,然后尝试再次使用缺货过滤器,它不返回任何结果。
我很难过。我认为这可能与过滤器逻辑在控制器中的工作方式有关,但我不明白为什么在将文本输入搜索后它没有读取项目数量值。
以下是“缺货”过滤器的自定义过滤器逻辑:
$scope.changeStockFilter = function() { // this function call on change checkbox value
if ($scope.searchInventory.qty === 0) {
$scope.exactMatching = true; // if qty 0 then set true
} else {
$scope.exactMatching = false;
}
};
以下是调用过滤器函数的方法
<div class="checkbox">
<input type="checkbox" name="filter" ng-model="searchInventory.inventory" data-ng-true-value="0" data-ng-false-value="''" ng-change="changeStockFilter()">
Out of Stock
</div>
这是过滤器应用于ngrepeat的方式
<div ng-repeat="item in inventory | filter: searchInventory : exactMatching | orderBy:sortOrder as filtered_result track by $index">
我还认为过滤器中的'track by'可能存在问题,假设我使用不正确。我在下面看了几页,我认为这不是问题所在。当然,我可能是错的。
我很感激有关这个问题的任何指导。谢谢!!!
答案 0 :(得分:2)
使用search
后,您的过滤器对象如下所示:
{ "qty": 0, "$": "" }
和exactMatching = true
一起返回没有结果,因为angular在这种情况下使用严格比较(angular.equals(actual, expected)
)而不是不区分大小写的子串匹配,并且由于这部分{{1 (这意味着应该至少有一个属性等于空字符串)。
您应该对"$": ""
事物使用单独的过滤器进行严格比较。其他过滤器应使用outOfStock
:
substring match in case insensitive way
过滤功能:
<div ng-repeat="item in inventory | filter: searchInventory | filter : outOfStockFilter | orderBy:sortOrder as filtered_result track by $index">