3

I want to achieve the following theoretical code:

VIEW.html

<li ng-repeat="player in players | filter:myCustomFilter(player)">{{player.name}}

CONTROLLER.js

// some theoretical conditional statement that return a boolean
$scope.otherCondition = true;


$scope.myCustomFilter = function(player) {
    return player.name.substring(0,1).match(/A/gi) && $scope.otherCondition;
}

So I want all of my players to be loaded into an Angular model, but I only want to render players into the DOM whose names start with the letter 'A'. When I try and do something like this, my console informs me that player is undefined. Do I need to write a custom filter in order to achieve this (via angular.module().filter())?

3 Answers 3

7

Here's a working fiddle: http://jsfiddle.net/orlenko/jV6DK/

Html code (exactly as Karl Zilles suggested):

<div ng-app>
    <div ng-controller="MyController">
        <h2>Names starting with "A":</h2>
        <ul>
            <li ng-repeat="player in players | filter:myCustomFilter">{{player.name}}</li>
        </ul>
        <h2>All Names:</h2>
        <ul>
            <li ng-repeat="player in players">{{player.name}}</li>
        </ul>
    </div>
</div>

Javascript:

function MyController($scope) {
    $scope.players = [{
        name: 'Arthur'        
    }, {
        name: 'William'
    }, {
        name: 'Bertha'
    }, {
        name: 'Alice'
    }];

    $scope.otherCondition = true;

    $scope.myCustomFilter = function(player) {
        return player.name.substring(0,1).match(/A/gi) && $scope.otherCondition;
    }
}

Result

Sign up to request clarification or add additional context in comments.

1 Comment

What if we want to pass 1 more parameter to the filter?
2

You don't need to pass player to the filter

<li ng-repeat="player in players | filter:myCustomFilter">{{player.name}}

Should work

Comments

0

The answers given are only partially correct, if you need to pass more arguments to the function you would need to create a closure and pass those arguments to the closure as follow:

The 'A' is passed to the closure and player is passed as a part of the context.

HTML:

<div ng-app>
    <div ng-controller="MyController">
        <h2>Names starting with "A":</h2>
        <ul>
            <li ng-repeat="player in players | filter:myCustomFilter('A')">{{player.name}}</li>
        </ul>
        <h2>All Names:</h2>
        <ul>
            <li ng-repeat="player in players">{{player.name}}</li>
        </ul>
    </div>
</div>

JS:

function MyController($scope) {
  $scope.players = [{
    name: 'Arthur'
  }, {
    name: 'William'
  }, {
    name: 'Bertha'
  }, {
    name: 'Alice'
  }];

  $scope.otherCondition = true;

  $scope.myCustomFilter = function(letter) {
    return function(player) {
      var rgxp = new RegExp(letter, "g");
      return player.name.substring(0, 1).match(rgxp) && $scope.otherCondition;
    }
  } 

}

Checkout a working jsfiddle

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.