Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 569
Daniel Donev Avatar asked Oct 30 '22 13:10

Daniel Donev


2 Answers

$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.

like image 63
Kunal Kakkad Avatar answered Nov 11 '22 12:11

Kunal Kakkad


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

like image 24
Venugopal Avatar answered Nov 11 '22 14:11

Venugopal