None
None

Reputation: 5670

Angularjs filter not working as expected

My code is like this

  $scope.AddressData.Address = [
            {
                "LocationId": 66927,
                "ClientId": 114,
                "UserId": 431
            },
            {
                "LocationId": 66928,
                "ClientId": 114,
                "UserId": 431
            },
            {
                "LocationId": 66929,
                "ClientId": 114,
                "UserId": 431
            },
            {
                "LocationId": 66930,
                "ClientId": 114,
                "UserId": 431
            }
        ];
        var found = $filter('filter')($scope.AddressData.Address, { LocationId: '66927' }, true);
        console.log(found);

All looks okay to me, But this always returns a null array. Can anyone point out what I am doing wrong?

Upvotes: 0

Views: 90

Answers (2)

Esteban Felix
Esteban Felix

Reputation: 1561

From the angular docs on filter, specifically the last argument to $filter:

comparator true: A shorthand for function(actual, expected) { return angular.equals(actual, expected)}. This is essentially strict comparison of expected and actual.

If you change your filter to:

$filter('filter')($scope.AddressData.Address, { LocationId: '66927' });

or:

$filter('filter')($scope.AddressData.Address, { LocationId: 66927 }, true);

It will work as expected.

Upvotes: 2

Wayne Ellery
Wayne Ellery

Reputation: 7958

Angular will do an exact type check when using filter when you pass in true. Instead use locationId as an int:

var found = $filter('filter')($scope.AddressData.Address, { LocationId: 66927 }, true);

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

Upvotes: 1

Related Questions