angular filter:{v:0} 不过滤任何东西。其他值正常工作

angular filter:{v:0} does not filter anything. Other values work properly

出于某种原因,这根本不会过滤任何东西。

这是select离子:

<select
    ng-model="s.id"
    ng-options="c.v as c.name for c in oarray | filter:{v:0}"
>
</select>

这是结构:

$scope.oarray = [{ name:"(select)", v:0 },{ name:"name1", v:100 },{ name:"name2", v:200 }];

select 框列出了所有元素:"(select)""name1""name2",即使过滤器说 return 仅 "(select)"因为它是唯一一个成员 v==0.

如果我将过滤器更改为 v:100,它将正确地过滤内容。

为什么?!如果这是 'feature' 或 'side effect' 那么在不更改已经预定义的值的情况下有什么解决方法?

提前致谢。

PS:这是你的游乐场:http://plnkr.co/edit/huRPv08A4bucJmcG60Fe?p=preview

您的过滤器正在搜索 v CONTAINS 0 的所有对象,而不是等于 0.

的所有对象

你需要发送:true给过滤器告诉它做一个严格的比较,例如

ng-options="c.v as c.name for c in oarray | filter:{v:0}:true"

这里的第二个参数是比较器,可选参数,默认为falseAngular documentation for filter 包含有关此参数的以下信息:

Comparator which is used in determining if values retrieved using expression (when it is not a function) should be considered a match based on the the expected value (from the filter expression) and actual value (from the object in the array).

Can be one of:

function(actual, expected): The function will be given the object value and the predicate value to compare and should return true if both values should be considered equal.

true: A shorthand for function(actual, expected) { return angular.equals(actual, expected)}. This is essentially strict comparison of expected and actual.

false: A short hand for a function which will look for a substring match in a case insensitive way. Primitive values are converted to strings. Objects are not compared against primitives, unless they have a custom toString method (e.g. Date objects).

Defaults to false.