【问题标题】:startWith filter with object in AngularJSAngularJS中带有对象的startWith过滤器
【发布时间】:2016-06-22 12:21:28
【问题描述】:

我正在尝试使用startWith 过滤器,但它没有正确过滤数据,例如P。让我知道我在这里做错了什么。

控制器代码 -

var app = angular.module('filterSample', []); app.controller('filterCtrl', function ($scope) {

$scope.data = {
    messages: [
        {       
        "id":"111619EEVz",
        "name":"Tom 67",
        "status":"Pending"
        },
        {
            "id":"115419EEAA",
            "name":"Business 34",
            "status":"Pending"
        },
        {   
            "id":"1B167000WW",
            "name":"Jack 78",
            "status":"Active",
        }
    ]
  }

  $scope.startsWith = function (actual, expected) {
    var lowerStr = (actual + "").toLowerCase();
    return lowerStr.indexOf(expected.toLowerCase()) === 0;
  }
});

HTML -

<div ng-controller="filterCtrl">
      <input type="text" ng-model="search" />
        <ul border="1px" ng-repeat="msg in data.messages | filter:search:startsWith">
          <li>{{msg.name}}</li>
        </ul>
    </div>

工作 Plnkr - http://plnkr.co/edit/tByYZ3jpEk7YPpJnMY11?p=preview

编辑 1 -

过滤应该只在name属性中搜索,而不是在整个对象中搜索。

【问题讨论】:

    标签: javascript angularjs


    【解决方案1】:

    将 ng-model 更改为 search.name

    <div ng-controller="filterCtrl">
      <input type="text" ng-model="search.name" />
        <ul border="1px" ng-repeat="msg in data.messages | filter:search:startsWith">
          <li>{{msg.name}}</li>
        </ul>
    </div>
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-13
    • 1970-01-01
    • 2015-12-06
    • 1970-01-01
    • 2017-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多