-4

I have a controller

app.controller('cartCtrl', function($scope) {

    $scope.$on('updateCart', function(event, cart) {
        $scope.cart = cart;
    });

    $scope.myFilter = function(item) {
        console.log(item);
    };
});   

The view which is loaded by routes :

<div class="shopping-row" data-ng-repeat="item in cart.items | filter:myFilter">
<div>

Now the problem is that myFilter is not triggred. If I call filter:myFilter() it gets triggered but value isn't passed.

I fail to see why this isn't working. Any help appreciated.

3 Answers 3

0

What if you use

filter:myFilter(item)

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

2 Comments

I am not getting item object.
That's because you didn't define a filter but a function to be called as a filter. Please check the official documentation on writing filters : docs.angularjs.org/api/ng/filter/filter
0

Error is there because you are trying to create a custom filter but you are doing it wrong.

This is how you create it.

angular.module('phonecatFilters', []).filter('checkmark', function() {
  return function(input) {
    return input ? '\u2713' : '\u2718';
  };
});

This is how you use it.

<dd>{{phone.connectivity.infrared | checkmark}}</dd>

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

4 Comments

You don't have to "ALWAYS" create a separate filter.
I wouldn't recommend that approach and neither should anyone. I would say keep your functionality separate and reusable.
Creating a separate filter each time you want check a specific item makes absolutely no sense.
0

This problem is cart.items has be an array for the filter function to be called, if it is a object it simply ignores it. ng-repeat would still display the object.

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.