Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude an object from an array in AngularJS filter?

I want to display a list of shops in an unordered list. I want to exclude "currentShop" from all shops. I wrote something like:

<ul class="dropdown-menu dropdown-menu-default">
                <li ng-repeat="shop in user.account.shops | filter:!currentShop}">
                    <a href="#/profile">
                        <i class="icon-user"></i> {{shop.name}} </a>
                </li>
            </ul>

Where am I wrong?

like image 252
Burak Avatar asked Apr 08 '15 19:04

Burak


People also ask

Which AngularJS filter selects a subset of items from an array?

AngularJS Filters filter Select a subset of items from an array. json Format an object to a JSON string. limitTo Limits an array/string, into a specified number of elements/characters. lowercase Format a string to lower case.

What is $filter in AngularJS?

Filters are used for formatting data displayed to the user. They can be used in view templates, controllers or services. AngularJS comes with a collection of built-in filters, but it is easy to define your own as well.

What is custom filter in angular?

Introduction to AngularJS Custom Filter. In AngularJS filters are used to modify or update the data before rendering the data on view or UI. Filters are clubbed in expression or directives using pipe (|) symbol.

How do I filter data in ng repeat?

The ng-repeat values can be filtered according to the ng-model in AngularJS by using the value of the input field as an expression in a filter. We can set the ng-model directive on an input field to filter ng-repeat values.


1 Answers

Here you have a working example. You can filter by a scope expression

ng-repeat="item in list | filter: myExpression"

Define your filter function;

$scope.myExpression= function(shop) {
  return shop.id !== $scope.current.id;
};
like image 94
Matias Fernandez Martinez Avatar answered Oct 02 '22 06:10

Matias Fernandez Martinez