Filter array of objects by attribute with integer value in ng-repeat

georgiy.zhuravlev picture georgiy.zhuravlev · Apr 24, 2015 · Viewed 8.2k times · Source

I have the directive like this

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} 
...
]

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?

thanks!

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)

SOLVED!!!

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 sorry, i can't vote for you answers - lack of reputation ... see you!

Answer

Arulkumar picture Arulkumar · Apr 24, 2015

You need to add the comparator as per the angular filter to achieve your requirement.

Can you change the code as:

ng-repeat="place in tb_places | filter: {'id' : inputID} : true"