-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.

Romain
  • 3,586
  • 7
  • 31
  • 52
Samosa
  • 845
  • 7
  • 19

3 Answers3

0

What if you use

filter:myFilter(item)

Tibo
  • 13
  • 4
  • I am not getting item object. – Samosa Aug 08 '14 at 09:24
  • 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 : https://docs.angularjs.org/api/ng/filter/filter – Romain Aug 08 '14 at 09:27
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

Harsh
  • 1,665
  • 1
  • 10
  • 16
  • 1
    You don't have to "ALWAYS" create a separate filter. – Samosa Aug 08 '14 at 09:35
  • See the answer http://stackoverflow.com/questions/16563018/custom-filters-and-ng-repeat-in-angularjs – Samosa Aug 08 '14 at 09:36
  • 1
    I wouldn't recommend that approach and neither should anyone. I would say keep your functionality separate and reusable. – Harsh Aug 08 '14 at 10:36
  • 1
    Creating a separate filter each time you want check a specific item makes absolutely no sense. – Samosa Aug 08 '14 at 12:14
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.

Samosa
  • 845
  • 7
  • 19