gpt4 book ai didi

javascript - Angular 过滤器 :{v:0} does not filter anything. 其他值工作正常

转载 作者:行者123 更新时间:2023-12-03 04:33:49 28 4
gpt4 key购买 nike

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

这是选择:

<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)""name1""name2",即使过滤器显示为仅返回 "(select)",因为它是唯一一个具有成员 v==0 的成员。

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

为什么?!如果这是一个“功能”或“副作用”,那么在不更改已经预定义的值的情况下,解决方法是什么?

提前谢谢您。

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

最佳答案

您的过滤器正在搜索 v 值包含 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.

关于javascript - Angular 过滤器 :{v:0} does not filter anything. 其他值工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43380825/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com