Angular JS filter not equals

穿精又带淫゛_ 提交于 2019-11-27 01:17:10

问题


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!


回答1:


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>



回答2:


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>


来源:https://stackoverflow.com/questions/22666366/angular-js-filter-not-equals

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!