Search code examples
angularjsangularjs-directiveangularjs-ng-repeat

How to do "equal to" rather than "start-with" in a AngularJs filter


I am new to AngularJs, I want to display categories and products under each category.

The product has a foreign key category_id which refers to its category.

I have tried these ways:

1.
      <tr ng-repeat="q in questions | filter: question_category_id == qc.id : true">
   
2. 
      <tr ng-repeat="q in questions | filter: question_category_id: question_category_id == qc.id : true">

3. 
      <tr ng-repeat="q in questions | filter: question_category_id == qc.id : true">

4. 
      <tr ng-repeat="q in questions | filter: {question_category_id: qc.id}">

none of them works correctly.

The 4th way is the closest but it displays like this:

category 1
  question1  (question_category_id = 1)
  question2  (question_category_id = 1)
  question3  (question_category_id = 10)

so that's why I think the filter is doing "start-with" job, rather than "equal-to" job.


Solution

  • filter takes a third argument to specify the comparator. Use true for a strict equals comparison (by default it does a substring match, which is what you're seeing)...

    <tr ng-repeat="q in questions | filter: {question_category_id: qc.id} : true">