Search code examples
angularjsangularjs-filter

AngularJS repeat filter strings ending with the letter G


I have an AngularJS app, and need to display object properties that end with a G on one line, and ones without the G on another:

<div class="col"><strong>Grass</strong>
  <span ng-repeat="(key, value) in player.ranking" ng- 
  filter="value.endsWith('G') = true">
  <span class="text-success">{{key.replace('Pool', '')}} {{value}}<span ng- 
  if="!$last">, &nbsp;</span></span></span>
</div>
<div class="col"><strong>Beach:</strong>
  <span ng-repeat="(key, value) in player.ranking" ng- 
  filter="!value.endsWith('G')">
  <span class="text-warning">{{key.replace('Pool', '')}} {{value}}<span ng- 
  if="!$last">,&nbsp; </span></span>
  </span>
</div>

What I have isn't working. Can someone please help me?


Solution

  • I am not sure where you got the ng-filter attribute from. However, this is not a valid filter for ng-repeat in AngularJS.

    The correct format is:

    item in items | filter:searchText
    

    In your HTML it looks something like:

     <ul ng-repeat="item in items | filter:query ">
          <li>{{item.name}}</li>
        </ul>
    

    So the quickest solution to your problem is to use a filter function

    html

     <ul ng-repeat="item in items |
          filter: filterFunction ">
          <li>{{friend.name}} ({{friend.age}})</li>
        </ul>
    

    JS

      $scope.filterFunction = function(value) {
          return value.endsWith('G');
        };