【问题标题】:AngularJS repeat filter strings ending with the letter GAngularJS 重复过滤字符串以字母 G 结尾
【发布时间】:2019-09-25 16:34:51
【问题描述】:

我有一个 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>

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

【问题讨论】:

    标签: angularjs angularjs-filter


    【解决方案1】:

    我不确定您从哪里获得 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');
        };
    

    【讨论】:

    • 谢谢!我最终意识到我做错了什么。在我发布之前,ng-filter 是 ng-if,因为当时我变得绝望并愿意尝试任何事情。我使用 ng-if="key.includes('G')" 显示草地排名,使用 ng-if="!key.includes('G')" 显示海滩排名,并且成功了!
    猜你喜欢
    • 1970-01-01
    • 2011-07-13
    • 1970-01-01
    • 2020-09-24
    • 2022-08-15
    • 1970-01-01
    • 1970-01-01
    • 2014-02-09
    • 2020-12-28
    相关资源
    最近更新 更多