Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Angularjs 1.1.5: ngRepeat not filtering correctly[UPDATE] #2797

Closed
doorman02 opened this issue May 25, 2013 · 12 comments
Closed

Angularjs 1.1.5: ngRepeat not filtering correctly[UPDATE] #2797

doorman02 opened this issue May 25, 2013 · 12 comments

Comments

@doorman02
Copy link

Hi,

The unstable version of Angularjs 1.1.5 is not filtering on attributes with value set to false.
Try this code as an example:

<!doctype html>
<html ng-app>
  <head>
    <script src="http://code.angularjs.org/1.1.5/angular.min.js"></script>
  </head>
  <body>
    <div ng-init="friends = [{name:'John', age:25, isMarried:false}, {name:'Mary', age:28, isMarried:true}]">
      I have {{friends.length}} friends. They are:
      Not married:
      <ul>
        <li ng-repeat="friend in friends | filter:friend.isMarried=false" >
          [{{$index + 1}}] {{friend.name}} who is {{friend.age}} years old.
        </li>
      </ul>
      Married:
      <ul>
        <li ng-repeat="friend in friends | filter:friend.isMarried=true" >
          [{{$index + 1}}] {{friend.name}} who is {{friend.age}} years old.
        </li>
      </ul>
    </div>
  </body>
</html>

If you change the Angularjs reference to the stable version 1.0.7 the filters work fine.

Is this a bug or am I doing something wrong?

Thanks

@vendethiel
Copy link

looks like your links / code examples disappeared

@doorman02
Copy link
Author

I just updated :)

@vendethiel
Copy link

  1. you're not supposed to close the attribute that early 2) it's ==

@doorman02
Copy link
Author

Sorry about that, I updated the example. This is still not working with the ==
However it works in the stable version.

Thanks!

@Grigore147
Copy link

+1 for this.

This doesn't work for me at least in v1.1.5 and v1.1.6. Looks to be an filter issue and not related to repeat.

App.controller('Todo', ['$scope', 'filterFilter', function($scope, filter) {
  var todos = [
    { title: 'One', completed: true },
    { title: 'Two', completed: false },
    { title: 'Three', completed: false },
    { title: 'Four', completed: false }
  ];

  var active = filter(todos, { completed: false }).length;

  console.log('active:', active); // outputs 4 instead of 3 active items
}]);

Can someone confirm this or we are doing something wrong?

Thanks.

@Grigore147
Copy link

Think i've found the issue and fix for it.
File src/ng/filter/filter.js:186 there is:

if (!expression[key]) return;

and should be:

if (typeof(expression[key]) === undefined) { return; }
// or
if (!expression.hasOwnProperty(key)) { return; }
// or
if (!(key in expression)) { return; }

Thanks.

@matt-d-rat
Copy link

+1 for this.

@vertex
Copy link

vertex commented Jul 31, 2013

+1 for this too

@zspecza
Copy link

zspecza commented Aug 13, 2013

+1 for this fix as well.

In the meantime, there is a handy tip from revolunet that will make filtering for falsey values a little easier:

$scope.isFalsey = function(val) {
  return !val;
}
things | filter:isFalsey

tomdcc added a commit to tomdcc/angular.js that referenced this issue Aug 15, 2013
Code was evaluating !expression[key] while attempting to
see if the key was present, but this was evaluating to true for
false values as well as missing keys.

Closes angular#2797.
@doorman02
Copy link
Author

Will this be fixed in angularJS 1.2? I tried filtering false property in angularjs 1.2.0-rc.3 but with no luck. Here is fiddle to test it:
http://jsfiddle.net/9ZWtt/

@dprentis
Copy link

You can see what works in different angular versions here: http://jsfiddle.net/f38zB/

Seems versions 1.1.3 to 1.2.0rc1 are most buggy. Version 1.2.0rc1 adds another bug into the fray. Versions 1.2.0rc2 and 1.2.0rc3 seem to work correctly using the object syntax. No version since 1.1.2 correctly handles the syntax given in the original example.

@doorman02
Copy link
Author

Cool thanks, object syntax worked :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants