0

I have an array show below,

$scope.selectionCat = ['1','3','4']

In HTML am calling like

<div dir-paginate="item in items | filterForCat:this">

Custom filter

.filter('filterForCat', function () {
  return function (item, scope) {
    return (scope.selectionCat.indexOf(item.category_id.id) !== -1);
  };
})

When am trying to read'scope.selectionCat' values am getting error like 'Cannot read property 'id' of undefined'

Can i get any help to solve this?

3
  • 2
    category_id is undefined. Have you verified that it exists on item? Commented May 11, 2017 at 6:26
  • yes i have verified.. when i console it am getting an array of objects.. Commented May 11, 2017 at 6:29
  • There is no such thing as this in a template. Don't pass the scope. Pass selectionCat. Commented May 11, 2017 at 6:31

1 Answer 1

1

Guess you want to filter items which's id is included by scope.selectionCat, as commented by @JB Nizet, you should not pass scope(this) to filters which you can but should avoid using filter this way, and you have to return filter result not just true/false of an expression.

Also for the error you are facing will be solved by Array.filter for if there's no data in items, the callback of Array.filter won't be executed.

Here you filter can be written like below

.filter('filterForCat', function () {
  return function (items, selectionVat) {
    return items.filter(function(item) {
      return selectionCat.indexOf(item.category_id.id) !== -1
    });
  };
})
Sign up to request clarification or add additional context in comments.

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.