87

Seems like a very basic question but I can't get the syntax right..

<li class="list-group-item" ng-repeat="question in newSection.Questions | filter:Id != '-1'; " ng-mouseenter="hover = true" ng-mouseleave="hover = false">
    <div href="#" editable-text="question.Text">{{question.Text}}</div>
</li>

All I want is to show all the questions where id is NOT -1. What am I doing wrong. Thanks!

3
  • you need to write a custom filter which takes checks the question.id != -1. Also I think your error is that id is an attribute of question Commented Mar 26, 2014 at 15:56
  • @EliteOctagon You can do it - see my answer below. Commented Mar 26, 2014 at 16:02
  • Dit you look at the documentation? docs.angularjs.org/api/ng/filter/filter In the part where expression is explained (second p at Usage -> arguments ) filter: {Id: "!-1"} or someting like that shoud do the trick Commented Sep 4, 2020 at 12:06

2 Answers 2

174

The syntax is just a little off, try:

<li class="list-group-item"
    ng-repeat="question in newSection.Questions | filter:{ Id: '!-1'}"
    ng-mouseenter="hover = true" ng-mouseleave="hover = false">

    <div href="#" editable-text="question.Text">{{question.Text}}</div>
</li>

See a little JSFiddle: http://jsfiddle.net/U3pVM/3845/

Edit:

Example with variables:

<script> var invalidId = '-1'; </script>
<li class="list-group-item"
    ng-repeat="question in newSection.Questions | filter:{ Id: '!' + invalidId}"
    ng-mouseenter="hover = true" ng-mouseleave="hover = false">

    <div href="#" editable-text="question.Text">{{question.Text}}</div>
</li>
Sign up to request clarification or add additional context in comments.

8 Comments

Can this somehow also work for variables? This slight modification of your fiddle places the "not-equal" quantity in a variable, instead of a constant, and it won't work anymore.
Well you probably need to writer your own filter function, let's call it checkForX and then use it like ... | filter:checkForX and return true or false if there is a match...
@Domi, i've posted a more general answer that answers your question. Check it out when you have the time
@Tielman, no need in declaring extra variables. This works well: "filter: {id: '!' + notValid}"
This will also filter out any Id which contains -1 as a substring, such as -11 or 2-1. To reliably check that the Id is not -1 you should write a small comparator function and use that as the filter.
|
21

Although @Michael Rose answer works for this case, i don't think it will if you try to filter not equal some object/variable

In that case you could use:

JS:

filterId = -1

HTML:

<li ng-repeat="question in newSection.Questions | filter: !{ Id: filterId}">
</li>

An even more nested case:

JS:

someQuestion = {
   someObject: {
     Id: 1
   }
}
newSection.Questions = [someQuestion]

HTML

<li ng-repeat="question in newSection.Questions | filter: !{someObject : {Id: -1} }">
</li>

2 Comments

This syntax doesn't work for me: !{ Id: -1} but the alternative answer works great: { Id:'!-1'}. Perhaps it is/was a version difference?
Could be. What version are you using?

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.