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

Generate error when attempting to use filter on objects #9992

Closed

Description

@justincy

Currently it fails silently if you try to use filter on objects, as in the example below, because it only supports arrays.

<div ng-repeat="item in object | filter:{display:true}">stuff</div>

I blew a ton of time on this because it's not very intuitive since ng-repeat supports objects. I fully expect to make the same mistake again. A simple error message would save a world of hurt.

Activity

  1. BrianMinister commented on Nov 11, 2014

    @BrianMinister

    I am concerned that if angular performs an is array check on every ng-repeat directive, one of the slowest directives in angular may get notably slower on more complex dashboard applications like mine where I have 2 or 3 layers of nested ng-repeats.

  2. caitp commented on Nov 11, 2014

    @caitp
    Contributor

    I am concerned that if angular performs an is array check on every ng-repeat directive, one of the slowest directives in angular may get notably slower on more complex dashboard applications like mine where I have 2 or 3 layers of nested ng-repeats.

    this is not likely to matter at all --- it's a one-time operation and it's a very small slice of the time (per set, as opposed to per item) taken to process that array in the first place. Even for nested repeats, the work isn't going to grow exponentially, it's really not going to matter

  3. caitp commented on Nov 11, 2014

    @caitp
    Contributor

    @justincy would you be interested in sending a PR for this? it should be a pretty trivial 1 line fix + a test case

  4. added this to the 1.3.4 milestone on Nov 11, 2014
  5. self-assigned this
    on Nov 11, 2014
  6. justincy commented on Nov 11, 2014

    @justincy
    ContributorAuthor

    @caitp I am interested but I've never looked at the angular.js source code so I can't promise anything.

  7. caitp commented on Nov 11, 2014

    @caitp
    Contributor

    If I'm not mistaken, it's basically this:

    function filterFilter() {
      return function(array, expression, comparator) {
    -   if (!isArray(array)) return array;
    +   if (!isArray(array)) throw $minErr('filter')('notarray', 'expected Array but received {0}'', array);
    
        var comparatorType = typeof(comparator),
            predicates = [];

    (and then you'd need to make up a 'notarray' error document in docs/content/error/filter/notarray.ngdoc --- but its not hard.

  8. caitp commented on Nov 11, 2014

    @caitp
    Contributor

    then again, I guess this would be a breaking change, so really you'd just want to log it --- even simpler

  9. reopened this on Nov 11, 2014
  10. caitp commented on Nov 11, 2014

    @caitp
    Contributor

    (oops, too much coffee)

  11. IShotTheSheriff commented on Nov 13, 2014

    @IShotTheSheriff
    Contributor

    Hi, I would be also interested in this issue, as it seems like a good place to start contributing adventure ;). I've started to work on that, but I couldn't make the tests passes. Could I ask for one more tip about how to use logging inside your source files? I've done it like this:

    filterFilter.$inject = ['$log'];
    function filterFilter($log) {
      return function(array, expression, comparator) {
       if (!isArray(array)) { 
         $log.error(new Error('filter expcted array but received ' + typeof array))
         return array;
       };
    

    I've tested the solution and I've added unit test and it passes. Unfortunately from ngMock unit tests I get error:

    Expected $log to be empty! Either a message was logged unexpectedly, or an expected log message was not checked and removed.

    If it wouldn't be a problem could you please point me gently to the right direction with this one?

  12. IgorMinar commented on Nov 22, 2014

    @IgorMinar
    Contributor

    I think that we should throw, but because that's a breaking change we should schedule this for 1.4

  13. modified the milestones: 1.3.4, 1.3.5 on Nov 25, 2014
  14. modified the milestones: , 1.3.5 on Nov 28, 2014
  15. Puigcerber commented on Dec 6, 2014

    @Puigcerber
    Contributor

    While creating this pull request I've noticed there are two protractor tests failing. The should auto compile for the example-example51 throws Expected 'name}}!' to be 'Angular!'. but is not related to these changes and fails for me as well in master. Could somebody try it out?

  16. mgcrea commented on Jul 13, 2015

    @mgcrea
    Contributor

    This type check broke promise usage with filters, the following expression is now throwing errors before resolving the promise:

    getAddress($viewValue) | filter:"Al" | limitTo:6
    
      $scope.getAddress = function() {
        return $q(function(resolve, reject) {
          resolve(['Alabama', 'Alaska', ...]);
        });
      }

    This broke AngularStrap typeahead with remote source example.

    Is there another way to achieve this?
    If not, I think we should revert this & remove the check/error (or at least allow promises).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions