Angularjs filter does not work when property is undefined

I have the following setting

$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 this:

  • this setting does not display anything

  • If I go to |filter :{propertyB: '!!'} :true , it will not display anything

  • if I change to |filter :{propertyB: undefined} :true , it displays everything

I can’t figure it out.

Purpose: I want to display elements that have propertyB undefined, and in the opposite case, the opposite.

Edit 1: if I angular.equals(item.propertyB, undefined) over an array using angular.equals(item.propertyB, undefined) , I get false, true, true

Edit 2: jsfiddle UPDATED

Edit 3: I updated the question

+5
source share
3 answers

I have finished it.

 .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"; } }; }]) 
0
source
 $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 $scope.filteredArray is the array you want, and you can use it in iterate for binding in html.

+1
source

you add filter to ng-repeat so that you get collection as an input to the filter instead of a single array element so that your implementation does not work.

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

check plnkr

+1
source

Source: https://habr.com/ru/post/1238614/


All Articles