5

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

4
  • 1
    Plese add jsfiddle or plunkr Commented Dec 18, 2015 at 9:42
  • 2
    This question has been answered here: stackoverflow.com/questions/25177004/… Commented Dec 18, 2015 at 9:52
  • 1
    Just remove true and everything works with ! and !!. Commented Dec 18, 2015 at 10:08
  • I have updated the question Commented Dec 22, 2015 at 14:28

3 Answers 3

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

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

Comments

1

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

1 Comment

Please check the updated question. Also i want to filter the elements before i render them (in the ng-repeat)
0

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";
            }
        };
    }])

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.