0

1. This is my supportDoc $scope:

var supportDoc = this;
......
supportDoc.docs = data;

enter image description here

2. My ng-repeat:

data-ng-repeat="doc in supportDoc.docs | $uniqueFilter: doc.icon"

3. And my filter:

/*@ngInject */
$uniqueFilter.$inject = [];
ng.module('app.Shared.Layout').filter('$uniqueFilter', $uniqueFilter);
function $uniqueFilter(input, key) {
    var unique = {};
    var uniqueList = [];
    for (var i = 0; i < input.length; i++) {
        if (typeof unique[input[i][key]] == "undefined") {
            unique[input[i][key]] = "";
            uniqueList.push(input[i]);
        }
    }
    return uniqueList;
     };

My filter is being reached but no matter what combination I use, eg.

| $uniqueFilter: icon
| $uniqueFilter: doc.icon
| $uniqueFilter: supportDoc.docs.icon

etc., I still receive the same error:

TypeError: Cannot read property 'length' of undefined at Object.$uniqueFilter (http://localhost:3250/app/dashboards/_shared/layout/filters/unique.client.filter.js:10:34) at Object.e [as invoke] (http://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.min.js:36:315) at Object.$get (http://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.min.js:34:268) at Object.e [as invoke] (http://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.min.js:36:315) at http://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.min.js:38:110 at Object.d [as get] (http://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.min.js:36:13) at $get [as $filter] (http://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.min.js:140:92) at ib.filter (http://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.min.js:193:186) at ib.filterChain (http://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.min.js:193:136) at ib.statements (http://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.min.js:192:441)

    <!-- ngRepeat: doc in supportDoc.docs | $uniqueFilter: icon 

The data is there. Removing the filter allows the data to be displayed property, albeit with duplicates that the filter was meant to be used to remove.

Any ideas? Thanks in advance.

6
  • Could you post the stacktrace please ? Commented Mar 3, 2015 at 14:14
  • First, the use of the filter is the first --> | $uniqueFilter: icon. You can forget the others Commented Mar 3, 2015 at 14:17
  • I tried that, it did not work. Commented Mar 3, 2015 at 14:18
  • Excuse me I am wrong ! It must it the model, so, the third : supportDoc.docs.icon Commented Mar 3, 2015 at 14:22
  • 1
    You should typically not use $name syntax since $--- usually signifies angular's internal methods/etc. Commented Mar 3, 2015 at 14:41

3 Answers 3

2

Filter function should return another function.

module.filter('$uniqueFilter', function () { return $uniqueFilter; })

Documentation : https://docs.angularjs.org/tutorial/step_09

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

Comments

0

Try using something like angular.forEach to avoid the error.

angular.forEach(input,function(row){
    if (typeof unique[row[key]] == "undefined") {
        unique[row[key]] = "";
        uniqueList.push(row);
    }
});

and try this :

ngRepeat: doc in supportDoc.docs | $uniqueFilter: 'icon' 

Comments

0

As mentionned Vinay K, your filter as to look at that :

/*@ngInject */
$uniqueFilter.$inject = [];
ng.module('app.Shared.Layout').filter('$uniqueFilter', $uniqueFilter);

function $uniqueFilter() {

    return function(input, key) {
        var unique = {};
        var uniqueList = [];
        angular.forEach(input,function(row){
            if (typeof unique[row[key]] == "undefined") {
                unique[row[key]] = "";
                uniqueList.push(row);
            }
        });
        return uniqueList;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.