在ng-repeat中按具有整数值的属性过滤对象数组 [英] Filter array of objects by attribute with integer value in ng-repeat
问题描述
我有这样的指令
ng-repeat="place in tb_places | filter:{'id':inputID}"
输出一些对象数组看起来像这样
to output some array of objects looks like this
$scope.tb_places = [
{name: 'some1', id:1}
...
{name: 'some10', id:10}
{name: 'some11', id:11}
{name: 'some12', id:12}
...
]
当我更改 inputID 并将其设置为等于 1 时,结果数组将填充源数组的元素,其中 'ids' 为 1 和 10,11,12.因此,'id' 值的部分像子字符串一样被检查,而不是数字.怎么治?
when i change the inputID and set it equal to 1, the result array fills with elements of source array with 'ids' of 1 and 10,11,12. Thus, the part of 'id' value is checked like substrings, not a numbers. How can i cure it?
谢谢!
更新我试图在过滤器表达式中添加:true" - 它完全清除输出(结果数组),它适用于简单的字符串数组,但不适用于对象(true"想要与模式对象完全匹配,这意味着它的所有属性)
UPD i've tried to add ":true" in filter expression - it completely clears output (result array), It works for a simple array of strings, but not the objects ("true" wants exact match with pattern object, it means with all its properties)
解决了!!!
对不起各位,我的错!'inputID' 与 'id' (string vs int) 的类型不同,因此内置比较器 (":true") 返回 false.非常感谢!
Sorry guys, my fault! 'inputID' was not the same type, as 'id' (string vs int), so built-in comparator (":true") returns false. Many thanks!
ps抱歉,我不能为你的答案投票 - 缺乏声誉......再见!
ps sorry, i can't vote for you answers - lack of reputation ... see you!
推荐答案
您需要按照 angular filter 来达到你的要求.
You need to add the comparator
as per the angular filter to achieve your requirement.
你能不能把代码改成:
ng-repeat="place in tb_places | filter: {'id' : inputID} : true"
这篇关于在ng-repeat中按具有整数值的属性过滤对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!