Angularjs filter negated

If using objects, you might also be interested in the following:

<li data-ng-repeat="obj in objs | filter:({obj_attr: '!obj_val'})">
   ...
</li>

Tested in AngularJS 1.1.5.


UPDATE: see ENDOH takanao's answer.

Looking at the AngularJS source code, it appears that '!' negates the result of the predicate, not the predicate itself:

var search = function(obj, text){
    if (text.charAt(0) === '!') {
       return !search(obj, text.substr(1));
    }
    switch (typeof obj) {
    ...

So, one way to work around this is to [If you don't like the '!'+myFilter syntax,] you can define your own predicate function in your controller:

$scope.inverseOriginFilter = function(item) {
    return item.search($scope.languageOrigin) == -1
}

Then use it in your HTML:

<select ng-model="languageDestination" ng-change="updatePrice()" 
    ng-options="language for language in languages | filter:inverseOriginFilter">
</select>

Example fiddle.


'!' character prepend to the filter string, like below:

filter:'!'+languageOrigin

<select ng-model="languageOrigin" ng-change="updatePrice()">
  <option ng-repeat="language in languages">{{language}}</option>
</select>
<select ng-model="languageDestination" ng-change="updatePrice()">
  <option ng-repeat="language in languages | filter:'!'+languageOrigin">{{language}}</option>
</select>

Tags:

Angularjs