0

I am trying to work with startWith filter but it is not filtering the data correctly, like in case of P. Let me know what I am doing wrong here.

Controller code -

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>

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

Edit 1 -

Filtering should be like it should only search in name property rather than searching in whole object.

1 Answer 1

2

change ng-model to 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>
Sign up to request clarification or add additional context in comments.

2 Comments

Thx a lot, its working now..btw can you please explain about the behavior :)
Please mark this as an answer. docs.angularjs.org/api/ng/filter/filter. Check the example there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.