Paul
Paul

Reputation: 26680

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

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

Upvotes: 12

Views: 18294

Answers (1)

bmleite
bmleite

Reputation: 26880

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/

Upvotes: 31

Related Questions