Daniel Donev
Daniel Donev

Reputation: 284

Angularjs Filter not working when the property is undefined

I have the following setup

  $scope.array = 
    [
      {propertyA: "test", 
       propertyB: {
                   propertyC: [true, true, false]
                  }
      },
      {propertyA: "test2"},
      {propertyA: "test3"}
    ]

and then

<div ng-repeat="item in array| filter :{propertyB: ''} :true">
     {{item.propertyA}}
</div>

So the problem is:

  1. this setup does not display anything

  2. if i change to |filter :{propertyB: '!!'} :true it does not display anything

  3. if i change to |filter :{propertyB: undefined} :true it displays everything

I can`t figure it out.

Target: I want to display the items which have the propertyB undefined and in other case the other way around.

Edit 1: If I iterate over the array with angular.equals(item.propertyB, undefined) I get false, true, true

Edit 2: jsfiddle UPDATED

Edit 3: I have updated the question

Upvotes: 5

Views: 541

Answers (3)

Daniel Donev
Daniel Donev

Reputation: 284

I endded up doing this.

.filter('undefinedProperties', ['$filter', function ($filter) {
        var checkProperty = function (property, returnUndefined) {
            if (returnUndefined) {
                if (property !== undefined) {
                    return true;
                } else {
                    return false;
                }
            } else {
                if (property === undefined) {
                    return true;
                } else {
                    return false;
                }
            }
        };
        return function (input, propertyArray, returnUndefined) {
            if (angular.isArray(propertyArray)) {
                if (angular.isArray(input) && input.length > 0) {
                    angular.forEach(propertyArray, function (property) {
                        for (var i = input.length; i-- > 0;) {
                            if (checkProperty(input[i][property], returnUndefined)) {
                                input.splice(i, 1);
                            }
                        }
                    });
                }
                return input;
            } else {
                throw "PropertyArray is not an array";
            }
        };
    }])

Upvotes: 0

Venugopal
Venugopal

Reputation: 1896

you are adding a filter on ng-repeat so you will get a collection, as input to the filter, instead of a single array element so your implementation will not work.

as Kunal mentioned, try filtering the array before hand and repeat on filtered array. or add a filter inside double curly braces {{}}.

check this plnkr

Upvotes: 1

Kunal Kakkad
Kunal Kakkad

Reputation: 653

$scope.array = 
[
  {propertyA: "test", propertyB: "test2"},
  {propertyA: "test2"},
  {propertyA: "test3"}
];
$scope.filteredArray =[];
angular.forEach($scope.array,function(eachData){
   if(angular.isUndefined(eachData.propertyB))
   $scope.filteredArray.push(eachData);
});

And the $scope.filteredArray is array you want and you can use it in repeat for binding in html.

Upvotes: 1

Related Questions