Angular.js: filter ng-repeat by absence in array

Paul picture Paul · Feb 12, 2013 · Viewed 18.2k times · Source

I need to filter items in ng-repeat so that only the items which not appear in alreadyAddedValues() array will be shown:

<ul class="dropdown-menu">
    <li ng-repeat="v in values() | filter: { ????? } ">{{value.name}}</li>
</ul>

$scope.values() = function(){
    ................
}

$scope.alreadyAddedValues() = function()
{
    //returns an array
}

The search of an already added value should perform by value.shortName

Answer

bmleite picture bmleite · Feb 13, 2013

You can, for example, use a custom function to do the filtering:

<li ng-repeat="v in values() | filter:filterAlreadyAdded ">{{value.name}}</li>

On the controller:

$scope.filterAlreadyAdded = function(item) {
    // filter logic here...
    // return false if item already added, true otherwise
};

jsfiddle: http://jsfiddle.net/bmleite/5VbCJ/