1

I'm developing an app using Angular 1.5.5. I'm trying to filter an array of Location objects by ID. The Locations array has 10 objects in it. The filter is returning 2 objects instead of the of the one:

Locations Array: enter image description here

My html template looks like:

                        <div class="col-md-3" ng-repeat="location in offerings.Locations | filter : {'ID':event.LocationID}">
                        {{location.Name}}<br />
                        {{location.Address}}, {{location.City}}, {{location.State}} {{location.Zip}}<br />
                        Loc ID = {{location.ID}} eventLocID = {{event.LocationID}}
                    </div>

It's returning 2 results instead of the filter where 2 = 2:

enter image description here

4
  • 1
    I am not sure, but it looks like it filter like strings, not numbers, you have got 12, because it contains 2 Commented Apr 20, 2017 at 13:46
  • I think this could be some issue related to event.LocationID or its type.. can you provide a plunker/fiddle reproducing this? Commented Apr 20, 2017 at 13:50
  • Thanks tanmay - it looks like that is the problem. Leguest's answer below solves that issue: ng-repeat="location in offerings.Locations | filter : {'ID':event.LocationID} : true" Commented Apr 20, 2017 at 17:47
  • @user1231748 did you checked my answer ? Commented Apr 20, 2017 at 18:56

2 Answers 2

2

You could try write own filter function or use that construction:

  ng-repeat="location in offerings.Locations | filter : {'ID':event.LocationID} : true"

Also check this topic Filter array of objects by attribute with integer value in ng-repeat

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

Comments

1

DEMO

var myApp = angular.module('myApp',[]);

myApp.controller('MyCtrl', function($scope) {
    $scope.event = {"LocationID" : 2}
    $scope.locations = [
     {
     "Address": "address1",
     "city": "city1",
     "ID": 2,
     "Name": "name1",
     "State": "state1",
     "Zip": 243435
     },
     {
     "Address": "address2",
     "city": "city2",
     "ID": 3,
     "Name": "name2",
     "State": "state2",
     "Zip": 243435
     },
      {
     "Address": "address3",
     "city": "city3",
     "ID": 12,
     "Name": "name3",
     "State": "state3",
     "Zip": 243435
     }    
    ];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl">
                    <div class="col-md-3" ng-repeat="location in locations | filter:{'ID' : event.LocationID}:true">
                        {{location.Name}}<br />
                        {{location.Address}}, {{location.City}}, {{location.State}} {{location.Zip}}<br />
                        Loc ID = {{location.ID}} eventLocID = {{event.LocationID}}
                    </div>
</div>

Comments

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.