Search code examples
angularjsangularjs-ng-repeatangularjs-filter

AngularJS filter true


I am trying to filter based on an accountID and I only want one record to come back at a time. I have the following code where if I set the filter: query: true, then no data shows. This happens when I add true to the ng-repeat. Why is it bringing back no records when I set it to true? Here is my code:

<div ng-controller="LookUpCtrl as vm">
<div>
    <input type="text" id="searchText" name="searchText" ng-model="query.accountId" />
</div>
 <div ng-if="query" ng-repeat="result in vm.results | filter: query : true">
        <div class="col-lg-4"><h2>{{result.accountName}}</h2></div>
        <div class="col-lg-4"><h2>[{{result.ordernumber}}]</h2></div>
        <div class="col-lg-4">Images go here</div>
  </div> 
<div>


Solution

  • After looking at your code, I can now see why your data is not appearing when filtering by the accountId. The value in your response object is an integer and the input for comparison in the text box is a string. This will never evaluate to true when doing a strict search. I was able to get it correctly filtering in your plunker when the accountId value is a string. There are a few new objects in your array testing that.

    {
      accountId: '3',
      ...
      name: 'Mr. This'  
    },
    {
      accountId: '4',
      ...
      name: 'Mr. That'
    },
    {
      accountId: '44',
      ...
      name: 'Mr. This'
    }
    

    Here is the updated link with your plunker (updated to load angular and the app). Please disregard my re-configuring of the controller structure..

    http://plnkr.co/edit/eZUayzldWmSEOjnkPceZ?p=preview