【问题标题】:Why AngularJS filter matches incomplete values为什么 AngularJS 过滤器匹配不完整的值
【发布时间】:2014-12-10 22:28:37
【问题描述】:

尝试使用 ng-model + 文本输入作为搜索栏时,我得到的值不完整。

作为一个例子:当我在文本输入中写“li”时,我得到的结果是“B Lifting”,但是当我尝试写“lifting”时它不会显示任何结果。如果我写“kl”,我可以找到“Gulklokken”,但如果我搜索“Gulklokken”,它不会显示结果。

我似乎无法弄清楚它的原因。

搜索栏代码:

   <div id="searchBar" class="input-group input-group-lg">
        <input type="text" ng-model="search.hovedkunde" class="form-control" placeholder="Search here" ng-focus="focused = true" ng-blur="focused = false">
    </div>

显示结果的代码:

        <div class="col-md-6">
            <div ng-if="search.hovedkunde" ng-controller="paginationCtrl">
                <table class="table table-hover">

                    <tr>

                        <th><a href="" ng-click="predicate = 'hovedkundE_ID'; reverse=false">ID</a></th>
                        <th><a href="" ng-click="predicate = 'hovedkunde'; reverse=false"> Hovedkunde</a></th>
                        <th><a href="" ng-click="predicate = 'lonG_NAME'; reverse=false">Kundenavn</a></th>
                        <th></th>

                    </tr>

                    <tr ng-repeat="pro in filtered = (projects | filter: search) | sliceArray:bigCurrentPage*pageSize | limitTo : pageSize | unique:pro.projectId | orderBy:predicate:reverse ">

                        <td ng-click="setInfo(pro.projectId)">{{pro.hovedkundE_ID}}</td>
                        <td ng-click="setInfo(pro.projectId)">{{pro.hovedkunde}}</td>
                        <td ng-click="setInfo(pro.projectId)">{{pro.lonG_NAME}}</td>
                        <td ng-click="setInfo(pro.projectId)"><button type="button" class="btn btn-primary">Retrieve</button></td>
                    </tr>

                </table>

                <div class="text-center">
                    <pagination total-items="bigTotalItems" ng-model="bigCurrentPage" max-size="maxSize" class="pagination" boundary-links="false" rotate="false" num-pages="numPages"></pagination>
                </div>

            </div>
        </div>

【问题讨论】:

  • 你能分享一个 jsfiddle 吗?
  • 嗯。该项目是针对客户的,涉及大量敏感数据。你需要什么小提琴?你看不出我发布的代码有什么问题吗?
  • 我没有足够的信息 - 你是如何取消在 (projects | filter: search) 中使用的“搜索”过滤器的
  • 搜索来自 ng-model。它没有在其他任何地方声明。应该是吗?从这个意义上说,它不是自定义过滤器,我只使用 ng-models 值作为过滤器。
  • 尝试使用$搜索所有属性。

标签: angularjs html angularjs-ng-repeat angular-ngmodel angular-filters


【解决方案1】:

filter 表达式允许您指定一个比较器


比较器,用于确定是否应将预期值(来自过滤器表达式)和实际值(来自数组中的对象)视为匹配。

可以是以下之一:

function(actual, expected): 函数将被赋予对象值和要比较的谓词值,如果该项目应包含在过滤结果中,则应返回 true。

true: function(actual, expected) { return angular.equals(expected, actual)} 的简写。这本质上是对预期和实际的严格比较。

false|undefined: 一个函数的简写,它将以不区分大小写的方式查找子字符串匹配。


在你的情况下,你应该像这样定义filter

filter:search.hovedkunde:true

【讨论】:

  • 我尝试使用 true,但这只会显示 100% 相等的值。就像我搜索“Soda”一样,它会返回所有名称为“Soda”的值,但不会显示“Sodapop”、“Sodabottles”等。也许 false|undefined 是我要找的?
  • 所以这只是 filter:search.hovedkunde:false?
  • 随意尝试多种选择,看看哪种方法适合您。
  • 感谢您帮助我更好地了解过滤器的工作原理。但问题似乎是来自 Angular UI 的分页。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-11-25
  • 1970-01-01
  • 2013-11-28
  • 1970-01-01
  • 2011-08-24
  • 1970-01-01
  • 2019-05-22
相关资源
最近更新 更多