Angular JS过滤器不等于

时间:2014-03-26 15:50:43

标签: javascript angularjs angularjs-ng-repeat ng-filter

似乎是一个非常基本的问题,但我无法正确使用语法..

<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>

我想要的只是显示id为非-1的所有问题。 我究竟做错了什么。谢谢!

2 个答案:

答案 0 :(得分:154)

语法稍微偏离,尝试:

<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>

看一点JSFiddle:http://jsfiddle.net/U3pVM/3845/

修改

变量示例:

<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>

答案 1 :(得分:19)

尽管@Michael Rose的回答适用于这种情况,但如果你试图过滤不等于某个对象/变量,我认为不会这样做

在这种情况下,你可以使用:

<强> JS

filterId = -1

<强> HTML:

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

更为嵌套的案例:

<强> JS

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

<强> HTML

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