按属性以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?
谢谢!
UPD 我试图在过滤器表达式中添加: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"(字符串与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!
推荐答案
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屋!