4

I have list of states and I have added search filter on name of states. I have array like this:

stateList : [{name: 'abc', area:500},{name: '!def', area:500}]

I have <li> with ng-repeat="state in stateList | filter:{name:searchText}"

Search text box with ng-model="searchText"

Search is working in normal scenario but when I search !(exclamation mark). It is not giving any result. It should give state with name '!def'

3
  • 1
    I suggest you to create your own custom filter Commented Dec 1, 2017 at 6:23
  • Any limitation or issue with angularjs search filter? Commented Dec 1, 2017 at 6:25
  • 1
    It seems AngularJs do a negation with !. take a look at stackoverflow.com/questions/29048673/… Commented Dec 1, 2017 at 6:27

2 Answers 2

2

The problem is that ! token is recognized by AngularJS as "negative predicate". Nevertheless you can create your custom myFilter like this:

angular.module('app', []).controller('ctrl', function($scope) {
  $scope.stateList = [{
    name: 'abc',
    area: 500
  }, {
    name: '!def',
    area: 500
  }]
}).filter('myFilter', function() {
  return function(input, filter) {
    if (!filter.name)
      return input;
    return input.filter(function(x) {
      return x.name.indexOf(filter.name) != -1;
    });
  }
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app='app' ng-controller='ctrl'>
  <input type='text' ng-model='searchText' />
  <ul>
    <li ng-repeat='state in stateList | myFilter : {name:searchText}'>{{state | json}}</li>
  </ul>
</div>

Sign up to request clarification or add additional context in comments.

Comments

0

Try this way

<input type="text" ng-model="state.name"/>

ng-repeat="state in stateList | filter:state"

2 Comments

I want to add filter on name property of state only. So I have to define filter:{name:searchText}.
Sorry. But didnt get your answer. Textbox is for search. right? Why did you write state.name in search textbox?

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.