0

I have a filter, that should use some variables from the controller. But when I run the application, there is an error in my console: Error: Can't find variable: $scope...

Here is my filter:

calcM.filter('money', function () {
    return function (premium) {
        if (isNaN(premium) || premium < 1) {
            return premium;
        } else {
            if ($scope.data.pickedOptions.currency == $scope.currency.USD) {
                return premium + 'USD'
            } else if ($scope.data.pickedOptions.currency == $scope.currency.EUR) {
                return premium + 'EUR'
            }
        }
    }
});

And then goes my controller:

calcM.controller('calcMCtrl', function($scope, $http, $window) {...});

If I include this filter in the controller I have another injection error.

P.Shustenko
  • 93
  • 13
  • 1
    You should pass variables/inputs into the filter when it is used in the view/template. See some of the existing filters as an example like [currency](https://docs.angularjs.org/api/ng/filter/currency). Don't rely on the filter automatically getting the value from the controller in which it is used. – Igor Apr 20 '17 at 14:26

1 Answers1

0

As said in the comment, you should pass arguments inside the filter:

calcM.filter('money', function () {
    return function (premium, currency, pickedOptions) {
        if (isNaN(premium) || premium < 1) {
            return premium;
        } else {
            if (pickedOptions.currency == currency.USD) {
                return premium + 'USD'
            } else if (pickedOptions.currency == currency.EUR) {
                return premium + 'EUR'
            }
            return premium;
        }
    }
});

See this answer on how to use multiple arguments in filter.

Community
  • 1
  • 1
Groben
  • 1,374
  • 2
  • 10
  • 29