0

I am trying to apply a filter to a numeric field as follows:

ng-repeat="venue in venues | filter:{ regionId: regionId }"

however, if for example, the regionId is 2, it will return venues for regionId's 2, 12, 23 etc.

Can anyone please advise the best way to resolve this?

2
  • What about writing a custom filter ? Commented Oct 31, 2014 at 8:08
  • Will resort to that if I have to. It just feels like this should be feasible. Commented Oct 31, 2014 at 9:12

2 Answers 2

2

OK, here is what I got to work:

ng-repeat="venue in venues | filter:{regionId:selectedRegionId}:true"

The problem is that the strict comparison only works for strings and not integer types. I had to convert regionId to a string first, but it made no difference what type selectedRegionId was.

Note also that the use of the strict comparator is Angular version-specific!

Sign up to request clarification or add additional context in comments.

2 Comments

For me I didn't have to convert to string. The values I had were numeric from a Json object and the strict comparison worked.
I edited the plunkr to highlight the usage of the correct datatype, see the filter by id (don't forget to toggle strict filtering): plnkr.co/edit/Z7aaMZzrMVBGMfrhL1aw?p=preview
0

The documentation says:

Usage

In HTML Template Binding

{{ filter_expression | filter : expression : comparator}}

[...]

comparator: function(actual, expected) | true | undefined

Comparator which is used in determining if the expected value (from the filter expression) and actual value (from the object in the array) should be considered a match.

Can be one of:

[...]

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

1 Comment

Thanks JB, but this use of strict is doing my head in. You will notice that the protractor test in the documentation doesn't actually work! I am trying to translate this into my code as follows: ng-repeat="venue in venues | filter:{ regionId: regionId } : true"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.