首页 > 解决方案 > AngularJS 重复过滤字符串以字母 G 结尾

问题描述

我有一个 AngularJS 应用程序,需要在一行上显示以 G 结尾的对象属性,在另一行上显示没有 G 的对象属性:

<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>

我所拥有的不起作用。有人可以帮帮我吗?

标签: angularjsangularjs-filter

解决方案


我不确定你ng-filter从哪里得到这个属性。但是,这不是AngularJSng-repeat中的有效过滤器。

正确的格式是:

item in items | filter:searchText

在您的 HTML 中,它看起来像:

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

因此,解决问题的最快方法是使用过滤器功能

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');
    };

推荐阅读