AngularJS: applying currency filter to return custom filter value

I am writing a custom filter to replace the zero value with "-" for display. However, if it is non-zero, it should return input, but be filtered as currency. When I tried this I got an unexpected token error.

angular.module('app')
        .filter('displaynullcurrency', function(){
            return function(input){
                if(!input){ return ' - '; }
                if(parseFloat(input) != 0){
                    return {{input | currency}};
                }
                return ' - ';
            };
        });

      

+3


source to share


2 answers


When you try to use $filter

javascript inside a function, you should use it like this:, the $filter('currency')(input)

syntax you used in your function is reserved for templates, try this instead:



angular.module('app')
        .filter('displaynullcurrency', function($filter){
            return function(input){
                if(!input){ return ' - '; }
                if(parseFloat(input) != 0){
                    return $filter('currency')(input);
                }
                return ' - ';
            };
        });

      

+5


source


You are trying to pass a value in a directive inside a binding statement. You can only do this in your templates.

To return an input filtered by currency, you should do something like this:



angular.module('app')
  .filter('displaynullcurrency', function($filter) {
    return function (input) {
      if (!input) { return ' - '; }
      if (parseFloat(input) != 0) {
        return $filter('currency')(input);
      }
      return ' - ';
    };
  });

      

+4


source







All Articles